D
D
DeadRayder2015-07-17 14:38:16
Android
DeadRayder, 2015-07-17 14:38:16

How to display information in the desired fragment?

Good afternoon everyone, please help me solve one problem. Namely, in the ListView, which is located in the fragment, I need to display data, I wrote an adapter for my needs, but I can’t assign this adapter to a specific ListView. Here is the error:

ArrayAdapterItem mArrayAdapterItem = new ArrayAdapterItem(this, R.id.liiiiiiiiiiiiiiiist,productCollection);

Swears at this, it turns out I'm passing the wrong Context. I just didn’t stuff it there, it doesn’t work and that’s it. The built-in adapters also don't work due to the same error. Help me figure out what needs to be created, redefined to insert this instead of this And it all worked) I'll attach a
few classes just in case:
package com.example.deadrayder_1.shopperguide;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;


public class HomeScreen extends FragmentActivity {

    static final String TAG = "MyLogs";
    static final int PAGE_COUNT = 3;

    ViewPager mViewPager;
    PagerAdapter mPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_screen);

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mPagerAdapter);

        mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Log.d(TAG,"onPageSelected, position = " + position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_home_screen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private class MyFragmentPagerAdapter extends FragmentPagerAdapter{

        public MyFragmentPagerAdapter(FragmentManager fm){
            super(fm);

        }

        @Override
        public CharSequence getPageTitle(int position) {
            if (position == 0)
            return "Товары";
            else if (position == 1)
                return "Корзина";
            else
                return "Акции";
        }

        @Override
        public Fragment getItem(int position) {
            return HomeScreenFragment.newInstance(position);
        }

        @Override
        public int getCount() {
            return PAGE_COUNT;
        }
    }
}

package com.example.deadrayder_1.shopperguide;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;


/**
 * A placeholder fragment containing a simple view.
 */
public class HomeScreenFragment extends Fragment {


    SimpleAdapter mSimpleAdapter;
    ListView mList;

    static final String ARGUMENT_PAGE_NUMBER = "arg_page_number";

    int pageNumber;
    int backColor;

    static HomeScreenFragment newInstance(int page) {
        HomeScreenFragment homefragment = new HomeScreenFragment();
        Bundle arguments = new Bundle();
        arguments.putInt(ARGUMENT_PAGE_NUMBER, page);
        homefragment.setArguments(arguments);
        return homefragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pageNumber = getArguments().getInt(ARGUMENT_PAGE_NUMBER);

        if (pageNumber == 1){
            Random rnd = new Random();
            backColor = Color.argb(40, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view;
        PorductItem[] productCollection = new PorductItem[2];
        productCollection[0] = new PorductItem("Wow");
        productCollection[1] = new PorductItem("Cio");

        view = inflater.inflate(R.layout.fragment,null);
        mList = (ListView) view.findViewById(R.id.liiiiiiiiiiiiiiiist);

        ArrayAdapterItem mArrayAdapterItem = new ArrayAdapterItem(, R.id.liiiiiiiiiiiiiiiist,productCollection);
     //   mList.setAdapter(mArrayAdapterItem);

        return  view;
    }
}

And here is the Adapter class
package com.example.deadrayder_1.shopperguide;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;


/**
 * Created by DeadRayder_1 on 17.07.2015.
 */
public class ArrayAdapterItem extends ArrayAdapter<PorductItem> {

    Context mContext;
    int layoutResourceId;
    PorductItem []data = null;

    public ArrayAdapterItem(Context p_mContext, int p_layoutResourceId, PorductItem[]p_data) {
        super(p_mContext,p_layoutResourceId,p_data);

        this.mContext = p_mContext;
        this.layoutResourceId = p_layoutResourceId;
        this.data = p_data;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null)
        {
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
            convertView = inflater.inflate(layoutResourceId, parent, false);
        }

        PorductItem objectItem = data[position];

        TextView mTextView = (TextView) convertView.findViewById(R.id.txt_product_name);
        mTextView.setText(objectItem.itemName);

        return convertView;
    }
}

Eee... error picturee8e8710331f1459081503f30b0f952d4.jpg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Emin, 2015-07-17
@DeadRayder

Your adapter requires Context as the first parameter. You should know that a Fragment, unlike an Activity, is not an ancestor of a Context. Therefore, you can pass the Activity in which this fragment is located to the adapter. You can get it from a fragment like this:
Then you can guess
UPD: but it crashes because you are not creating the adapter correctly. In ArrayAdapter, you need to pass the id of the text field, and it’s not clear what (R.id.liiiiiiiiiiiiiiiiist).
Read the guide , come back when you've mastered it.

D
DeadRayder, 2015-07-17
@DeadRayder

What is meant by fragment here? The only value that the adapter takes is just GetActivity(), the program starts, but crashes when the adapter is created.
Thank you, I read the article, redid everything as it should, it works

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question