2012-03-26 119 views
1

我創建的自定義適配器包含文本和圖像,在特定的列表項我不得不打開另一個活動的點擊列表視圖,但我不能開火的ListView點擊事件,下面是我的代碼。Listview點擊不火。

謝謝。

適配器

public class ImageAndTextAdapter extends ArrayAdapter<String> { 

private LayoutInflater mInflater; 
private Context myCtx; 
private String[] mStrings; 
private TypedArray mIcons; 

private int mViewResourceId; 

public ImageAndTextAdapter(Context ctx, int viewResourceId, 
     String[] strings, TypedArray icons) { 
    super(ctx, viewResourceId, strings); 
    myCtx = ctx; 
    mInflater = (LayoutInflater)ctx.getSystemService(
      Context.LAYOUT_INFLATER_SERVICE); 

    mStrings = strings; 
    mIcons = icons; 

    mViewResourceId = viewResourceId; 
} 

@Override 
public int getCount() { 
    return mStrings.length; 
} 

@Override 
public String getItem(int position) { 
    return mStrings[position]; 
} 

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

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    convertView = mInflater.inflate(mViewResourceId, null); 

    ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon); 
    iv.setImageDrawable(mIcons.getDrawable(position)); 


    TextView tv = (TextView)convertView.findViewById(R.id.option_text); 
    tv.setText(mStrings[position]); 

    return convertView; 
} 
} 

主胡亞蓉

public class OurWishingWellActivity extends ListActivity implements OnClickListener{ 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // setContentView(R.layout.list_view_image); 

    Context ctx = getApplicationContext(); 
    Resources res = ctx.getResources(); 

    String[] options = res.getStringArray(R.array.reg_item_names); 
    TypedArray icons = res.obtainTypedArray(R.array.reg_icons); 

    setListAdapter(new ImageAndTextAdapter(ctx, R.layout.list_item, 
      options, icons)); 

} 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 

} 


} 
+0

你有沒有任何ImageBut噸或按鈕在你的清單項目? – user936414 2012-03-26 04:49:35

+0

你能告訴我們你的主要xml和行xml嗎? – RobinHood 2012-03-26 04:52:34

+0

我要給你發送整個..只是有一段時間 – Ishtiaq 2012-03-26 04:55:15

回答

0

尊敬Aafag#

你的onclick的ListView寫像下面。

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    // TODO Auto-generated method stub 
    super.onListItemClick(l, v, position, id); 
    Toast.makeText(AdvancedListViewActivity.this, "working", Toast.LENGTH_SHORT).show(); 
    System.out.println("working!!!!!!!!!"); 
} 
+0

是的,你是對的,但我是新來的這 你已經張貼了關於列表視圖。在我的情況下沒有在任何XML列表,只是一個文本視圖和圖像查看它創建在運行時,我需要一個onItemClick事件,可以調用一些其他活動.. 這是實際的例子http://androidcookbook.com/Recipe.seam;jsessionid=5424397F3130CE7769FF47DD67742911?recipeId=1418&recipeFrom=ViewTOC – Ishtiaq 2012-03-26 06:03:54

+0

是的,我實施這個例子,添加這幾行代碼後,它爲我工作,它是否也爲你工作? – RobinHood 2012-03-26 06:21:36

+0

它提供了一個錯誤在FindViewById ... :( – Ishtiaq 2012-03-26 06:42:47

0

添加聽者

@Override 

公共視圖getView(INT位置,查看convertView,父的ViewGroup){ convertView = mInflater.inflate(mViewResourceId,null);

ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon); 
iv.setImageDrawable(mIcons.getDrawable(position)); 


TextView tv = (TextView)convertView.findViewById(R.id.option_text); 
tv.setText(mStrings[position]); 

convertView .setOnClickListener(新OnClickListener(){

 @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      startActivity(); 


     } 
    }); 

return convertView; 

}