設置onClicks我試圖作出在ListView項目的onlcicklistener,但我不知道從哪裏開始。我嘗試了一些東西,但都沒有工作。下面是類代碼:自定義列表視圖
public class Primary_Weapons extends Activity {
private ListView listView1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.we_primary_list);
Weapon weather_data[] = new Weapon[]
{
new Weapon(R.drawable.we_braton, "Braton"),
new Weapon(R.drawable.we_braton_prime, "Braton Prime"),
new Weapon(R.drawable.we_braton_vandal, "Braton Vandal"),
new Weapon(R.drawable.we_braton, "MK1-Braton"),
new Weapon(R.drawable.we_boltor, "Boltor")
};
WeaponAdapter adapter = new WeaponAdapter(this,
R.layout.listview_item_row, weather_data);
listView1 = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
}
}
當用戶點擊一個項目,它的文本將定義一個字符串,那麼該字符串將在另一種方法來使用。例如,如果選擇「Braton總理」,這將設置一個字符串「Braton總理」,那麼將被用來做其他的東西
編輯: 適配器代碼:
public class WeaponAdapter extends ArrayAdapter<Weapon>{
Context context;
int layoutResourceId;
Weapon data[] = null;
public WeaponAdapter(Context context, int layoutResourceId, Weapon[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
WeatherHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new WeatherHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
row.setTag(holder);
}
else
{
holder = (WeatherHolder)row.getTag();
}
Weapon weather = data[position];
holder.txtTitle.setText(weather.title);
holder.imgIcon.setImageResource(weather.icon);
return row;
}
static class WeatherHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}
編輯3: 基本上我想在ListView的每個項目做不同的事情
感謝這一點,但如何將我去的問題中提及設置字符串? – cw1998
@ cwilkinson1998你能證明你的適配器代碼? – Raghunandan
@Raghunandan如果您編輯自己的帖子,請做正確! – Benoit