ListView OnClickListener未提供所需的结果

时间:2012-10-31 08:15:40

标签: android listview onitemclicklistener

我想使用ListView ClickListener获取ListView中所选子项的名称,但它总是将项目的位置返回为0,这是不正确的,因为我在列表视图中有5个子项。这是列表视图:

private static org.secure.sms.Main.DiscussArrayAdapter adapter;

    ListView childView = (ListView)findViewById(R.id.listViewChild);
    adapter = new DiscussArrayAdapter(getApplicationContext(), R.layout.listitem_discuss);
    childView.setAdapter(adapter);
    adapter.add(new OneComment(true,c,cn.getTime()));


    childView.setOnItemClickListener(new AdapterView.OnItemClickListener()                      
    public void onItemClick(AdapterView<?> parent, View v, int position, long id)
    {                                              
    CreateAlertDialog(parentFileAddress);
    String a = childView.getAdapter().getItem(position).toString();
    }});

这是我将值传递给适配器的类:

public class OneComment {
    public boolean left;
    public String comment;
    public long timeLeft;

    public OneComment(boolean left, String comment,long timeLeft) 
    {
        super();
        this.left = left;
        this.comment = comment;
        this.timeLeft = timeLeft;
    }
}

这是处理适配器功能的类:

public class DiscussArrayAdapter extends ArrayAdapter<OneComment> {

    private TextView countryName;
    private List<OneComment> countries = new ArrayList<OneComment>();
    private LinearLayout wrapper;
    public View row;

    @Override
    public void add(OneComment object) 
    {
        try
        {
            countries.add(object);
            super.add(object);
        }
        catch(Exception e)
        {
            Log.e("Exception: ",e+" Exception occured in add() of DiscussArrayAdapter.java");
        }
    }

    public DiscussArrayAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    public int getCount() 
    {
        try
        {
        return this.countries.size();
        }
        catch(Exception e)
        {
            Log.e("Exception: ",e+" Exception occured in getCount() of DiscussArrayAdapter.java");
        }
        return this.countries.size();
    }

    public OneComment getItem(int index)
    {
        try
        {
        return this.countries.get(index);
        }
        catch(Exception e)
        {
            Log.e("Exception: ",e+" Exception occured in getItem() of DiscussArrayAdapter.java");
        }
        return null;        
    }

    public View getView(int position, View convertView, ViewGroup parent) 
    {
        try
        {
        row = convertView;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.listitem_discuss, parent, false);
        }

        wrapper = (LinearLayout) row.findViewById(R.id.wrapper);

        OneComment coment = getItem(position);

        countryName = (TextView) row.findViewById(R.id.comment);

        countryName.setText(coment.comment);

        TextView TimeLeft = (TextView) row.findViewById(R.id.timeleft);

        int hours,minutes,seconds; 

        if(coment.timeLeft > 0) // When the time is greater than 0
        {
            if(TimeLeft == null)
            {
                ((ViewGroup) row).addView(TimeLeft);
            }

            long t = coment.timeLeft;

            seconds = (int) (t  / 1000) % 60 ;
            minutes = (int) (t / (1000*60)) % 60;
            hours = minutes / 60;

            TimeLeft.setText("Time Remaining: "+hours+" : "+minutes+" : "+seconds); 
        }
        else
        {
            if(TimeLeft != null)
            {
                ((ViewGroup)TimeLeft.getParent()).removeView(TimeLeft); 
            }   
        }

        wrapper.setGravity(coment.left ? Gravity.LEFT : Gravity.RIGHT);

        return row;
        }
        catch(Exception e)
        {
            Log.e("Exception: ",e+" Exception occured in View getView(int position, View convertView, ViewGroup parent) of DiscussArrayAdapter.java");
        }
        return row;
    }
}

请帮帮我。谢谢。

3 个答案:

答案 0 :(得分:1)

 childView.setOnItemClickListener(new AdapterView.OnItemClickListener()                      
    public void onItemClick(AdapterView<?> parent, View v, int position, long id)
    {                                              
    CreateAlertDialog(parentFileAddress);
    String a = childView.getAdapter().getItem(position).toString();
    }}); 

修改上述代码如下: -

childView.setOnItemClickListener(new OnItemClickListener()                      
    public void onItemClick(AdapterView<?> parent, View v, int position, long id)
    {                                              
    CreateAlertDialog(parentFileAddress);
    String a = childView.getItemIdAtPosition(position).toString();
    }}); 

答案 1 :(得分:0)

//包含onclick的数组适配器。

public class DiscussArrayAdapter extends ArrayAdapter<OneComment> {


    private Vector<OneComment> countries;
    Context context;

    public View row;

    /**
     * Method just to test the items
     */
    public void AddTestItems(){

        for(int i = 0; i<5 ; i++){
            OneComment comment = new OneComment(true, "India"+ i, System.currentTimeMillis());
            countries.add(comment);
        }
    }

    public DiscussArrayAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        countries = new Vector<OneComment>();
        //Remove it f you do not want to perform test cases. Just to test the listview.
        AddTestItems();
        this.context = context;
    }

    @Override
    public void add(OneComment object) {
        countries.add(object);
        super.add(object);
    }

    @Override
    public int getCount() {
        return countries.size();
    }

    @Override
    public OneComment getItem(int position) {
        return countries.elementAt(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        row = convertView;
        Holder holder = null;
        if(row == null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.listitem_discuss, parent, false);
            holder = new Holder();
            holder.countryName =  (TextView) row.findViewById(R.id.comment);
            holder.wrapper = (LinearLayout) row.findViewById(R.id.wrapper);
            holder.timeLeft = (TextView) row.findViewById(R.id.timeleft);
            row.setTag(holder);
        }else{
            holder = (Holder) row.getTag();
        }

        OneComment coment = countries.get(position);

        holder.countryName.setText(coment.comment);

        int hours,minutes,seconds; 

        if(coment.timeLeft > 0) // When the time is greater than 0
        {
            holder.timeLeft.setVisibility(View.VISIBLE);
            long t = coment.timeLeft;

            seconds = (int) (t  / 1000) % 60 ;
            minutes = (int) (t / (1000*60)) % 60;
            hours = minutes / 60;

            holder.timeLeft.setText("Time Remaining: "+hours+" : "+minutes+" : "+seconds); 
        }
        else
        {
            holder.timeLeft.setVisibility(View.GONE);
        }

        holder.wrapper.setGravity(coment.left ? Gravity.LEFT : Gravity.RIGHT);

        row.setOnClickListener(new OnClickListenerOneComment(position));

        return row;
    }

    /**
     * Class which sets the click listener for the rows in the list.
     */
    public class OnClickListenerOneComment implements OnClickListener{

        int position;

        public OnClickListenerOneComment(int position){
            this.position = position;
        }

        public void onClick(View v) {
            //Perform whatever you wanna do on click here.
            Toast.makeText(context, position + "", Toast.LENGTH_SHORT).show();

        }

    }

    /**
     * Class to create a holder for all the inflated elements for your view.
     * Best way to do it.
     *
     */
    static class Holder{

        TextView countryName;
        TextView timeLeft;
        LinearLayout wrapper;
    }


}

答案 2 :(得分:-1)

试试这个:

static ArrayList<OneComment> oneComment;

现在在您的项目中点击:

public void onItemClick((AdapterView<?> parent, View v, int position, long id) 
{
   String a = oneComment.get(pos).comment;
}

您可以获取该位置的listitem值。