如何在水平列表视图中设置特定项目textview和imageview中的文本和图像

时间:2015-03-18 04:56:32

标签: android listview android-fragments android-listview

我已将片段中的值传递给mainactivity,同时单击片段类中的按钮。我的主要活动有一个片段和一个水平列表视图与基础适配器。我的水平列表视图有一个imageview和一个textview。我想在特定的水平列表项中设置值和图像。如果我必须单击片段类中的第一项加上按钮,则值和图像要设置水平列表视图的第一项。请在这里建议me.my水平listview适配器代码。提前谢谢。

public class HorizontalAdapter extends BaseAdapter {

private Context context;
int value = 0;
int a[];

public HorizontalAdapter(Context c, int value) {
    context = c;
    this.value = value;
}

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

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

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

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

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        // convertView = new View(context);
        convertView = inflater.inflate(R.layout.horizantal_image, null);
    }
    TextView textView = (TextView) convertView.findViewById(R.id.title);
    ImageView imageView = (ImageView) convertView.findViewById(R.id.image);

    textView.setText(String.valueOf(value));
    // imageView.setImageResource(image);
    System.out.println(value);
    System.out.println(position);
    System.out.println(convertView);
    System.out.println(parent);
    // textView.setText(dataObjects[position]);
    // imageView.setImageResource(imageid[position]);

    return convertView;

}

}

1 个答案:

答案 0 :(得分:0)

您必须传递一些对象或ArrayList数组根据您的要求并更改数组的特定项的值并将其传递给您的适配器。您可以创建一个模型类,您可以在其中定义文本和图像

 public class ListModel{
        private String imageUrl;
        private String title;

        public String getImageUrl() {
            return imageUrl;
        }

        public void setImageUrl(String imageUrl) {
            this.imageUrl = imageUrl;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }
    }

在BaseAdapter中你必须像这样使用ListModel的ArrayList:

ArrayList<ListModel> listData = new ArrayList<ListModel>();

你必须从像这样的地方设置listData的内容

ListModel listModelData = new ListModel();
// Add Content for specific object
listModelData.setTitle("your title");
listModelData.setImageUrl("your image url");


// Add this object in listview 
listData.add(listModelData);

之后,您必须在BaseAdapter中传递此列表,如下所示:

public class HorizontalAdapter extends BaseAdapter {

private Context context;
private  int value = 0;
private ArrayList<ListModel> listData;

public HorizontalAdapter(Context c, int value, ArrayList<ListModel>   listData) {
    context = c;
    this.value = value;
   this.listData= listData;
}

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

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

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

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

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        // convertView = new View(context);
        convertView = inflater.inflate(R.layout.horizantal_image, null);
    }
    TextView textView = (TextView) convertView.findViewById(R.id.title);
    ImageView imageView = (ImageView) convertView.findViewById(R.id.image);

    textView.setText(String.valueOf(listData.get(position).getImageUrl()));
    // imageView.setImageResource(image);
    System.out.println(String.valueOf(listData.get(position).getTitle()));
    System.out.println(position);
    System.out.println(convertView);
    System.out.println(parent);
    if (listData.get(position).getImageUrl()!=null){
      imageView.setImageResource(listData.get(position).getImageUrl());
    }

    if (listData.get(position).getTitle()!=null){
       textView.setText(listData.get(position).getTitle());
    }



    return convertView;

}
}

要更新ArrayList的具体位置,您必须这样做:

listData.get(0).setImageUrl("Updated Image url");
listData.get(0).setTitle("Updated Title ");

要在listview中设置适配器,您必须从您的活动中执行此操作:

HorizontalAdapter  mAdapter = new HorizontalAdapter (mCtx, value,listData );
mListViews.setAdapter(mAdapter);

要更新Listview,您必须在更新ArrayList的内容后调用notifyDataSetChanged,如下所示:

if (mAdapter !=null){
mAdapter .notifyDataSetChanged();
}