0
我想在單擊列表項時打開一個新的Activity,我在Layoutinfaltor的幫助下使用了一個CustomAdapter類來顯示列表項。 這裏是我的代碼listview onitemclicklistener with layoutinflater
Exercise.class
public class Excercise extends Activity {
public String taskName[] = { "Table Tennis", "Basketball", "Badminton",
"Swimming", "Gymnastic", "Bike Riding", "Mountain Climbing",
"Horse Riding" };
public String startTiem[] = { "8:50 AM", "9:30 AM", "10:AM", "11:00 AM",
"5:00 PM", "5:30 PM", "2:00 PM", "6:00 PM" };
public String duration[] = { "10 mins", "15 mins", "30 mins", "15 mins",
"10 mins", "15 mins", "2 hours", "30 mins" };
ListView lv;
Context context;
TextView categorie;
int value;
String categorie_Name;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_excercise);
savedInstanceState = getIntent().getExtras();
categorie = (TextView) findViewById(R.id.tvheadder);
categorie_Name = savedInstanceState.getString("Categorie Name");
categorie.setText(categorie_Name);
context = this;
lv = (ListView) findViewById(R.id.items);
lv.setAdapter(new CustomAdapter(this, taskName, startTiem, duration));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(getApplicationContext(), AddTask.class);
startActivity(i);
}
});
}
public void addTask(View v) {
Intent i = new Intent(getApplicationContext(), AddTask.class);
i.putExtra("Categorie Name", categorie_Name);
startActivity(i);
}
public void onClickHelp(View v) {
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setContentView(R.layout.activity_help);
ImageView dialogButton = (ImageView) dialog.findViewById(R.id.btncross);
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
這些語句不執行,我想打開一個新的活動被點擊我的列表視圖時
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(getApplicationContext(), AddTask.class);
startActivity(i);
}
});
CustomAdapter.class
public class CustomAdapter extends BaseAdapter{
String [] name,time,timeDruation;
Context context;
private static LayoutInflater inflater=null;
public CustomAdapter(Excercise mainActivity, String[] taskName, String[] startTime,String[] duration) {
name=taskName;
context=mainActivity;
time=startTime;
timeDruation=duration;
inflater = (LayoutInflater)context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return name.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder
{
TextView taskName,startTime,duration;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.activity_listview_items,null);
holder.taskName=(TextView) rowView.findViewById(R.id.tvTaskName);
holder.startTime=(TextView) rowView.findViewById(R.id.tvStartTime);
holder.duration=(TextView) rowView.findViewById(R.id.tvDuration);
holder.taskName.setText(name[position]);
holder.startTime.setText(time[position]);
holder.duration.setText(timeDruation[position]);
rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "You Clicked "+name[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
的'onItemClick'在鍛鍊活動不叫刪除此代碼,但'onClick'在適配器是什麼? – Karim
請嘗試從自定義適配器類刪除rowview的OnClick偵聽器。 –