我想使用 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;
}
}
请帮助我。在此先感谢。