如何在ListView中添加图像?

时间:2013-12-13 23:04:49

标签: java android eclipse android-listview

我有一个带过滤搜索的Android列表视图,但我不知道如何为每个项目的列表视图添加图像。我尝试了以下代码,但它无法正常工作。该程序不会在列表视图中显示包含项目的图像。我该怎么办?

import android.os.Bundle;
import android.app.ListActivity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ListActivity {
    String[] str = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"};     
    ArrayAdapter<String> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, str);
        setListAdapter(adapter);

        //test image
        //ImageView img = (ImageView) findViewById(R.id.imageView1);
        //str [2] = str[2]+img;

        //Search In ListView
        EditText ed = (EditText) findViewById(R.id.editText1);
        ListView lv = (ListView) findViewById(android.R.id.list);
        lv.setTextFilterEnabled(true);

        ed.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
                MainActivity.this.adapter.getFilter().filter(arg0);
            }
        });         
    }

    public View getView(int position, View convertView, ViewGroup parent) {
         ImageView myImageView = new ImageView(); 

           switch(position) {
              case 0: myImageView.setImageResource(R.drawable.bp);
              break;

              case 1: myImageView.setImageResource(R.drawable.bp);
              break;

              case 2: myImageView.setImageResource(R.drawable.bp);
              break;
            }

            return myImageView;
        }

    //Click Listener
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id){
        super.onListItemClick(l, v, position, id);

        Toast.makeText(this, adapter.getItem(position) +" Your Selected ",
                Toast.LENGTH_SHORT).show();

        if (adapter.getItem(position).equals("One")) {
            startActivity(new Intent(MainActivity.this, One.class));
        } else if (adapter.getItem(position).equals("Two")) {
            startActivity(new Intent(MainActivity.this, Two.class));
        }
    }

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

2 个答案:

答案 0 :(得分:0)

我会为listView创建一个自定义适配器。为此adater创建布局并向其添加imageView。

答案 1 :(得分:0)

你应该在getview中使用intent,因为你只是返回没有父视图的imageview