2013-12-13 115 views
0

我有一個安卓列表視圖與篩選搜索,但我不知道如何爲每個項目的列表視圖添加圖像。我嘗試了下面的代碼,但它沒有正常工作。該程序不顯示列表視圖中的項目的圖像。我該怎麼辦?如何在ListView中添加圖像?

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; 
    } 
} 

回答

0

我會爲我的listView創建一個自定義適配器。爲這個adater創建一個佈局,併爲其添加一個imageView。

0

你應該在getview中使用intent,因爲你只是返回沒有父視圖的imageview