listview onitemclicklistener with layoutinflater

Posted

技术标签:

【中文标题】listview onitemclicklistener with layoutinflater【英文标题】: 【发布时间】:2015-08-01 10:17:35 【问题描述】:

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

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();


这些语句没有执行,我想在我的列表视图被点击时打开一个新的Activity

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;

【问题讨论】:

ExerciseActivity中的onItemClick没有被调用,但是Adapter中的onClick被调用了? 请尝试从自定义适配器类中删除 rowView 的 OnClick 侦听器。 【参考方案1】:

您应该在 CustomAdapter 中删除此代码

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

【讨论】:

以上是关于listview onitemclicklistener with layoutinflater的主要内容,如果未能解决你的问题,请参考以下文章

vb listview控件问题

ListView 包含 ListView

在一个listview中嵌套ListView可以吗?

求助scrollview嵌套viewpager 放listview 不能滑动

c# listview 怎么添加图片

Android listview 的用法? 越详细越好。