多行ListView左侧有ImageView

时间:2012-02-22 18:37:12

标签: java android

我需要创建一个类似于ff的项目。 PIC。 (我为使用siily pic而道歉,我必须使用随时可用的东西)。
enter image description here


我不会说我做的是制作“动态”ListView的最佳方法,但我很高兴能够制作出我目前的作品。无论如何,我的实现如下:

public class MultiLineListViewActivity extends Activity {

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

    ArrayList<SearchResults> searchResults = getSearchResults();

    ListView listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(new MyCustomBaseAdapter(this, searchResults));
}

private ArrayList<SearchResults> getSearchResults() {
    ArrayList<SearchResults> results = new ArrayList<SearchResults>();

    SearchResults sr1 = new SearchResults();
    sr1.setName("Name 1");
    sr1.setPhone("12345");
    SearchResults.setIcon(R.drawable.pic_one);
    results.add(sr1);

    sr1 = new SearchResults();
    sr1.setName("Name 2");
    sr1.setPhone("123456");
    SearchResults.setIcon(R.drawable.pic_two);
    results.add(sr1);

    ...

    return results;
}
}


    公共类SearchResults {

private String name;
private String phone;
public static ArrayList<Integer> iconsList = new ArrayList<Integer>();

public void setName(String name) {
    this.name = name;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public static void setIcon(int i) {
    iconsList.add(i);
}

public String getName() {
    return this.name;
}

public String getPhone() {
    return this.phone;
}
}


public class MyCustomBaseAdapter extends BaseAdapter {

private ArrayList<SearchResults> searchArrayList;
private LayoutInflater layoutInflater;
private Context context;

public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {
    this.searchArrayList = results;
    //this.context = context;
    layoutInflater = LayoutInflater.from(context);
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    ImageView imageView;

    if(convertView == null) {
        convertView = layoutInflater.inflate(R.layout.custom_row_view, null);

        holder = new ViewHolder();
        holder.txtName = (TextView) convertView.findViewById(R.id.name);
        holder.txtPhone = (TextView) convertView.findViewById(R.id.phone);

        imageView = new ImageView(context);

        convertView.setTag(holder);
    }
    else {
        //imageView = (ImageView) convertView;
        holder = (ViewHolder) convertView.getTag();
    }

    imageView = new ImageView(context);
    imageView.setImageResource(SearchResults.iconsList.get(position));

    holder.txtName.setText(searchArrayList.get(position).getName());
    holder.txtPhone.setText(searchArrayList.get(position).getPhone());

    return convertView;
}

class ViewHolder {
    TextView txtName;
    TextView txtPhone;
}


上面的代码成功地在ListView中显示名称电话行,这是添加了“强制关闭”的图像。
< BR />

记下注释行 imageView =(ImageView)convertView; 我已将其减少到该特定行,因为删除注释会导致应用程序崩溃。

2 个答案:

答案 0 :(得分:3)

注意:每当你在StackOverflow问题中得到一个强制关闭时,请发布完整的堆栈跟踪。

你总是创建一个新的ImageView,实际上你可能根本不需要创建一个TextView。它应该在您的自定义项目布局中定义(在这种情况下您应该发布),就像findViewById一样。您可以以相同的方式使用{{1}}并获取它。

答案 1 :(得分:3)

kabuko在两个方面都是正确的。 (更新:下面的评论也不应该被忽略。)我猜你的layout/custom_row_view是一个LinearLayout,这意味着评论的行试图强迫一个LinearLayout到一个ImageView,是的会引发一个异常。此外,您的ViewHolder类是多余的,只需将这些TextView定义为getView()中的变量,就像这样......

// Assuming that layout/custom_row_view is a LinearLayout...
getView(yadda) {
   LinearLayout row;
   ImageView imageView;
   TextView txtName, txtPhone;

   if (convertView == null) {
      row = // inflate a new one
   } else {
      row = // get the old one
   }

   // fetch the views
   imageView = row.findViewById(...);
   txtName = row.findViewById(...);
   txtPhone = row.findViewById(...);

   // fill the views
   imageView.setImageResource(...);
   txtName.setContent(...);
   txtPhone.setContent(...);

   return row;
}