listview onitemclicklistener with layoutinflater

时间:2015-05-20 09:04:56

标签: android android-activity android-listview android-adapter android-inflate

我想在单击列表项时打开一个新的Activity,我使用CustomAdapter类在Layoutinfaltor的帮助下显示列表项。 这是我的代码

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

}

1 个答案:

答案 0 :(得分:0)

您应该在CustomAdapter中删除此代码

rowView.setOnClickListener(new OnClickListener() {            
    @Override
    public void onClick(View v) {
        Toast.makeText(context, "You Clicked "+name[position], Toast.LENGTH_LONG).show();
    }
});