如何与列表中的行中的对象进行交互

时间:2012-11-18 12:57:15

标签: android list textview

我的应用中有一个列表。列表中的每一行都有一个图像和一个textView。

当单击相应的行时,我希望textView向右移动。

如何在"setOnItemClickListener"

中“获取”textView

这是我的适配器:

public class MyAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private TextView textView;
private ImageView icon;
public boolean first;

public MyAdapter(Context context, String[] values) {
    super(context, R.layout.rowlayout, values);
    this.context = context;
    this.values = values;
    this.first = true;
}

@SuppressWarnings("deprecation")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
    textView = (TextView) rowView.findViewById(R.id.row_text_view);
    icon = (ImageView) rowView.findViewById(R.id.icon);
    if (first) {
        rowView.setBackgroundColor(Color.TRANSPARENT);
        textView.setBackgroundColor(Color.TRANSPARENT);
        icon.setBackgroundColor(Color.TRANSPARENT);
        first = false;
        rowView.setClickable(false);
        rowView.setEnabled(false);
    } else {
        rowView.setBackgroundColor(Color.WHITE);
        textView.setBackgroundColor(Color.WHITE);
        icon.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_launcher));
    }
    textView.setText(values[position]);

    return rowView;
}}

谢谢!

1 个答案:

答案 0 :(得分:4)

OnItemClickListener具有以下签名的方法:

onItemClick(AdapterView<?> parent, View view, int position, long id)

所以你点击了行用户。您现在可以从行获取文本视图:

view.findViewById(R.id.row_text_view)`.
相关问题