-1

i want to read a xml file and show the items in a ListView,,,,now i can do it and after it i want to alternate row colors in ListView and i find a solution in this topic ,,,but i have a problem with getView method,,,what is my problem?

public class MainActivity extends Activity {
ArrayAdapter<App> adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EditText EDSearch= (EditText) findViewById(R.id.EDSearch);
    ListView listView = (ListView) findViewById(R.id.listView1);

    List<App> apps = null;
    try {
        XMLPullParserHandler parser = new XMLPullParserHandler();
        InputStream is=getAssets().open("main.xml");
        apps = parser.parse(is);

       adapter =new ArrayAdapter<App>
                (this,android.R.layout.simple_list_item_1, apps);
        listView.setAdapter(adapter);
    }
    catch (IOException e) {e.printStackTrace();}

    //Text Changed for search in ListView
    listView.setTextFilterEnabled(true);
    EDSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            MainActivity.this.adapter.getFilter().filter(s);
        }
    });
}

// fill the listview by xml file
public class App {

    private int id,location;
    private String title,activtiy,address;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getActivtiy() {
        return activtiy;
    }
    public void setActivtiy(String activtiy) {
        this.activtiy = activtiy;
    }
    public int getLocation() {
        return location;
    }
    public void setLocation(int location) {
        this.location = location;
    }

    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return id + "\n " + title + "\n" +activtiy+"\n"+location+"\n"+address;
    }

//Here is my problem,,,cant reolve getView

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        if (position % 2 == 1) {
            view.setBackgroundColor(Color.BLUE);
        } else {
            view.setBackgroundColor(Color.CYAN);
        }
        return view;
    }
}

public class XMLPullParserHandler {

    private List<App> root= new ArrayList<App>();
    private App app;
    private String text;

    public List<App> getApps() {
        return root;
    }

    public List<App> parse(InputStream is) {

        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser  parser = factory.newPullParser();

            parser.setInput(is, null);

            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String tagname = parser.getName();
                switch (eventType) {
                    case XmlPullParser.START_TAG:
                        if (tagname.equalsIgnoreCase("row")) {
                            app = new App();
                        }
                        break;

                    case XmlPullParser.TEXT:
                        text = parser.getText();
                        break;

                    case XmlPullParser.END_TAG:
                        if (tagname.equalsIgnoreCase("row")) {
                            root.add(app);
                        }else if (tagname.equalsIgnoreCase("id")) {
                            app.setId(Integer.parseInt(text));
                        }
                        else if (tagname.equalsIgnoreCase("title")) {
                            app.setTitle(text);
                        }
                        else if (tagname.equalsIgnoreCase("activity")) {
                            app.setActivtiy(text);
                        }
                        else if (tagname.equalsIgnoreCase("location")) {
                            app.setLocation(Integer.parseInt(text));
                        } else if (tagname.equalsIgnoreCase("address")) {
                            app.setAddress(text);
                        }
                        break;

                    default:
                        break;
                }
                eventType = parser.next();
            }

        } catch (XmlPullParserException e) {e.printStackTrace();}
        catch (IOException e) {e.printStackTrace();}

        return root;
    }
}

this is my layout file

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.asaravani.sqlserver.MainActivity">

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="500px"
    android:divider="#d8d8d8"
    android:dividerHeight="1dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/EDSearch">

</ListView>

<EditText
    android:id="@+id/EDSearch"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:drawableRight="@android:drawable/ic_menu_search"
    android:ems="10"
    android:hint="کلمه مورد نظر را وارد کنید" >

    <requestFocus />
</EditText>

Community
  • 1
  • 1
Ali Saravani
  • 25
  • 1
  • 2
  • 7

2 Answers2

0

You need a to write a CustomAdapter to do this. The getView method belongs to an adapter and not an Activity. You are trying to write the getView method inside the activity, which is the cause of your error.

This is how you create a custom adapter.

K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33
0

You need to implement an custom adapter for that. I give you an example and i have put in the change of background color for each list item and it works fine. Remember to use viewHolder for each list item components.

public class EarnRedeemAdapter extends BaseAdapter {
    private Context context;
    private List<GroupData<Merchant>> sortMerchants = new ArrayList<>();
    private Transformation transformation;
    private boolean earnFlag = true;

    public EarnRedeemAdapter(Context context, List<GroupData<Merchant>> list) {
        this.context = context;
        this.sortMerchants = list;
        UtuAppRunningStorage.wrapperMerchantList.clear();
    }

    public EarnRedeemAdapter(Context context, List<GroupData<Merchant>> list, boolean earnFlag) {
        this.context = context;
        this.sortMerchants = list;
        this.earnFlag = earnFlag;
        UtuAppRunningStorage.wrapperMerchantList.clear();
    }

    @Override
    public int getCount() {
        return sortMerchants.size();
    }

    @Override
    public Object getItem(int position) {
        return sortMerchants.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_earn_redeem, parent, false);
            viewHolder.iv_item_earn_redeem_merchant_icon = (ImageView) convertView.findViewById(R.id.iv_item_earn_redeem_merchant_icon);
            viewHolder.tv_item_earn_redeem_merchant_points = (TextView) convertView.findViewById(R.id.tv_item_earn_redeem_merchant_points);
            viewHolder.tv_item_earn_redeem_merchant_name = (TextView) convertView.findViewById(R.id.tv_item_earn_redeem_merchant_name);
            viewHolder.tv_item_earn_redeem_merchant_category = (TextView) convertView.findViewById(R.id.tv_item_earn_redeem_merchant_category);
            viewHolder.tv_item_earn_redeem_merchant_distance = (TextView) convertView.findViewById(R.id.tv_item_earn_redeem_merchant_distance);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (position % 2 == 1) {
            convertView.setBackgroundColor(Color.BLUE);
        } else {
            convertView.setBackgroundColor(Color.CYAN);
        }

        if (merchant != null) {
            if (!TextUtils.isEmpty(merchant.getImage())) {
                Picasso.with(context)
                        .load(merchant.getImage())
                        .placeholder(R.drawable.icon_background_white)
                        //.transform(new CircleTransform())
                        .transform(new RoundedTransformation(4, 1))
                        .into(viewHolder.iv_item_earn_redeem_merchant_icon);
            }

            //System.out.println("---> merchant.getRewardsdesc(): " + merchant.getRewardsdesc());
            if (TextUtils.isEmpty(merchant.getRewardslistingdesc())) {
                viewHolder.tv_item_earn_redeem_merchant_points.setVisibility(View.GONE);
                viewHolder.tv_item_earn_redeem_merchant_points.setBackgroundColor(Color.TRANSPARENT);
            } else {
                viewHolder.tv_item_earn_redeem_merchant_points.setVisibility(View.VISIBLE);
                viewHolder.tv_item_earn_redeem_merchant_points.setText(merchant.getRewardslistingdesc());
            }

            viewHolder.tv_item_earn_redeem_merchant_name.setText(merchant.getMerchantname());
            viewHolder.tv_item_earn_redeem_merchant_category.setText(merchant.getCategorydesc());


            return convertView;
        }


    }
    class ViewHolder {
        private ImageView iv_item_earn_redeem_merchant_icon;
        private TextView tv_item_earn_redeem_merchant_points;
        private TextView tv_item_earn_redeem_merchant_name;
        private TextView tv_item_earn_redeem_merchant_category;
        private TextView tv_item_earn_redeem_merchant_distance;
    }
}
kggoh
  • 742
  • 1
  • 10
  • 24
  • if (position % 2 == 1) { convertView.setBackgroundColor(Color.BLUE); } else { convertView.setBackgroundColor(Color.CYAN); } – kggoh Jul 27 '16 at 07:37
  • you have to implement a custom adapter, which suit your own list item. Only you have your adapter ready, in your activity, you can call adapter = new EarnRedeemAdapter(getActivity(), list, true); listview_fragment_earn_redeem.setAdapter(adapter); – kggoh Jul 27 '16 at 07:37