如何获得三个TextView值?

时间:2019-03-05 06:46:37

标签: android listview textview

我的列表视图具有三个文本视图和一个水平排列的复选框。

我的listview xml代码

<TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:text="TEXT"
    android:id="@+id/textView1"
    android:textSize="20sp"
    android:gravity="center"
    android:layout_weight="5" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:text="TEXT"
    android:id="@+id/textView2"
    android:textSize="20sp"
    android:gravity="center"
    android:layout_weight="10" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:text="TEXT"
    android:id="@+id/textView3"
    android:textSize="20sp"
    android:gravity="center"
    android:layout_weight="5" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/checkBox1"
    android:focusable="false"
    android:clickable="false"
    android:layout_gravity="right"
    android:layout_marginRight="10dp"/>

MainActivity.java代码

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView,
                                View view, int position, long id) {

            Object vo = (Object)adapterView.getAdapter().getItem(position);  
            Log.d(TAG, String.valueOf(vo));
        }
    });

String.valueOf(vo)的日志输出是

sn.studyroom.ListViewItem@f085546

如何获取三个Textview值中的每一个?

3 个答案:

答案 0 :(得分:1)

如果正确捕获了单击的对象,则必须将其转换为TextView,然后像这样获取TextView的文本:

String vo = ((TextView)adapterView.getAdapter().getItem(position)).getText().toString();
Log.d(TAG, vo);

OR

转到您的适配器类并创建一个接口来获取被单击项的值。

答案 1 :(得分:0)

您可以使用适配器访问文本字段和复选框。将您的列表传递给这样的适配器:

CustomListAdapter adapter = new CustomListAdapter (context, yourList);
        yourListView.setAdapter(adapter);

创建这样的适配器。

public class CustomListAdapter extends BaseAdapter {

    Context context;
    List<ListObject> yourList;


    public CustomListAdapter (Context context, List<ListObject> yourList) {
        this.context = context;
        this.yourList= yourList;
    }

    public void refreshAdapter(List<ListObject> yourList){
        this.yourList= yourList;
        notifyDataSetChanged();
    }

    private static class ViewHolder {
        TextView textView1, textView2, textView3;
        CheckBox checkBox1;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return yourList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return yourList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ViewHolder holder;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.your_adapter_layout, null);
            holder = new ViewHolder();

            holder.textView1= convertView.findViewById(R.id.textView1);
            holder.textView2= convertView.findViewById(R.id.textView2);
            holder.textView3= convertView.findViewById(R.id.textView3);
            holder.checkBox1= convertView.findViewById(R.id.checkBox1);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();

        }
       //TODO: you can do whatever you want to do here


        return convertView;
    }
}

答案 2 :(得分:0)

如果您要检索列表数据,那么我想知道有关设置数据的信息。您是在设置模型并添加适配器还是采用静态字符串arraylist?所以让我知道,然后我将建议更好的方法。