如何在不使用Viewholder的情况下仅过滤自定义适配器中的Brand?

时间:2015-08-24 00:47:12

标签: android

我正在创建一个具有自定义列表视图和自定义列表视图适配器的Android应用程序。如何在不使用viewholder的情况下使用自定义listview适配器仅过滤品牌名称?可能吗?如果是,我该如何过滤它?

Listviewadapter.java

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter {

// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();

public ListViewAdapter(Context context,
                       ArrayList<HashMap<String, String>> arraylist) {
    this.context = context;
    data = arraylist;
    imageLoader = new ImageLoader(context);
}

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

@Override
public Object getItem(int position) {
    return null;
}

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

public View getView(final int position, View convertView, ViewGroup parent) {
    // Declare Variables
    TextView Brand;
    TextView Category;
    TextView Code;
    TextView Description;
    TextView Price;
    TextView Availability;
    ImageView Image;

    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View itemView = inflater.inflate(R.layout.activity_main, parent, false);
    // Get the position
    resultp = data.get(position);

    // Locate the TextViews in listview_item.xml
    Brand = (TextView) itemView.findViewById(R.id.Brand);
    Category = (TextView) itemView.findViewById(R.id.Category);
    Code = (TextView) itemView.findViewById(R.id.Code);
    Description = (TextView) itemView.findViewById(R.id.Desscription);
    Price = (TextView) itemView.findViewById(R.id.Price);

    // Locate the ImageView in listview_item.xml
    Image = (ImageView) itemView.findViewById(R.id.list_image);

    // Capture position and set results to the TextViews
    Brand.setText(resultp.get(MainActivity.Brand));
    Category.setText(resultp.get(MainActivity.Category));
    Code.setText(resultp.get(MainActivity.Code));
    Description.setText(resultp.get(MainActivity.Description));
    Price.setText(resultp.get(MainActivity.Price));
    // Capture position and set results to the ImageView
    // Passes flag images URL into ImageLoader.class
    imageLoader.DisplayImage(resultp.get(MainActivity.Image), Image);
    // Capture ListView item click

    Availability = (TextView) itemView.findViewById(R.id.Availability);
    if(Availability.getText().equals("Available")){

        Availability.setTextColor(Color.GREEN);

    }
   return itemView;
}



}

1 个答案:

答案 0 :(得分:0)

您不应该在listviewadapter中进行任何信息过滤,而是在主类中进行任何信息过滤,然后在处理完信息后将信息传输到listviewadapter

这是我的,我所做的只是

 public class LVAAdjust extends BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;

public LVAAdjust(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data = d;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public void ListViewAdapter2(Context context,
        ArrayList<HashMap<String, String>> d) {

    data = d;
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

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

public class ViewHolder {
    TextView txtFirst;
    TextView txtSecond;
    TextView txtThree;
}


private int selectedItem;

// is used outside the adapter for example OnItemClickListener
public void setSelectedItem(int position) {
    selectedItem = position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;

    ViewHolder holder;
    if (convertView == null) {
        vi = inflater.inflate(R.layout.list_item, null);

        convertView = inflater.inflate(R.layout.adjustcols, null);
        holder = new ViewHolder();

        holder.txtFirst = (TextView) convertView
                .findViewById(R.id.TextFirst);
        holder.txtSecond = (TextView) convertView
                .findViewById(R.id.TextSecond);
        holder.txtThree = (TextView) convertView
                .findViewById(R.id.TextThree);

    } else {

        holder = (ViewHolder) convertView.getTag();
    }

    HashMap<String, String> map = data.get(position);


  String fir = (map.get(FIRST_COLUMN) == null) ?
  "NA" :     map.get(FIRST_COLUMN);
    String sec = (map.get(SECOND_COLUMN) == null) ?
   "NA" : map.get(SECOND_COLUMN);
    String thi = (map.get(THIRD_COLUMN) == null) ? 
  "NA" : map.get(THIRD_COLUMN);
    try {
                holder.txtFirst.setText(fir);
    holder.txtSecond.setText(sec);
    holder.txtThree.setText(thi);
    holder.txtFirst.setHeight(40);
    holder.txtFirst.setTextSize(18);

    holder.txtSecond.setHeight(40);
    holder.txtSecond.setTextSize(18);

    holder.txtThree.setHeight(40);
    holder.txtThree.setTextSize(18);
    } catch (Exception e) {
        // TODO: handle exception
    }


    return convertView;
}
}