从android中的listview适配器获取TextView

时间:2014-11-24 06:18:37

标签: android android-listview

我有什么: 我在列表视图的每一行都有文本视图

我要做的是:

  • 点击列表中的文字视图,我想得到的名称 该特定行的设定值
  • 我想从类本身检测click事件而不是 适配器

CODE ::

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listviewdemo);

        final ListView listviewone = (ListView) findViewById(R.id.listviewone);
        listviewone.setAdapter(mAdapterOne);

        listviewone.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

                Log.d("", "");                  

            }
        });
    }

适配器的XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#fff"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textColor="#000" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:scaleType="centerCrop"
        android:padding="5dp"
        android:src="@drawable/img" />

     <ImageView
        android:id="@+id/imageLine"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/line" />



</LinearLayout>

6 个答案:

答案 0 :(得分:5)

view是列表项,因此您可以执行

String str =(String) ((TextView) view.findViewById(R.id.txt)).getText();

答案 1 :(得分:1)

((TextView) view.findViewById(R.id.title).getText().toString()

但是我建议你从适配器本身获取文本,因为适配器将用于填充ListView。

答案 2 :(得分:1)

如果你想在textview上设置值,那就试试吧..

 listviewone.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

               TextView mText  = (TextView) view
                    .findViewById(R.id.title);      

               String value= mText.getText().ToString().trim();            

            }
        });

答案 3 :(得分:0)

从ListView数据持有者处获取特定列表项的名称:

示例:

ArrayList<HashMap<String,String>> listData= new ArrayList<HashMap<String, String>>();
HashMap<String,String> row1=  new HashMap<String, String>();
row1.put("name","AAA");
listData.add(row1);

listviewone.setOnItemClickListener(new AdapterView.OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
          Log.d("Name :", listData.get(position).get("name"));                  
     }
});

注意: listData用于ListView数据持有者,它是传递适配器以构建ListView项目。

答案 4 :(得分:0)

尝试这样,

 private String getName(int pos){
                View view = listviewone.getAdapter().getView(pos, null, listviewone);
                TextView name = (TextView) view.findViewById(R.id.title);

                return name.getText().ToString();
    }

    listviewone.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

                    Log.d("NAME", getName(position));                  

                }
            });

希望它会帮助你

答案 5 :(得分:0)

interface上使用TextView 点击活动

代码段

用于处理点击事件的

接口

public interface ListItemBtnClickInterface {

    public void listItemBtnClickListener(Object obj, int viewId, String value);
}

我正在使用ArrayAdapter

适配器构造函数的代码

public AddExpTypeAdapter(Context context, int resource,
        List<ExpenseTypeInfo> objects, ListItemBtnClickInterface _interface) {
    super(context, resource, objects);
    // TODO Auto-generated constructor stub
    _context = context;
    layoutId = resource;
    dataList = objects;
    this._interface = _interface;
    notifyDataSetChanged();
}
初始化并设置文本

后, 适配器中的 TextView点击的代码

vieweHolder.urView.setTag(position);
vieweHolder.urView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Object data = v.getTag();
            if(data != null) {
                String value = vieweHolder.urView.getText().toString();
                _interface.listItemBtnClickListener(data, v.getId(), value);
            }
        }
    });

ListViewActivity设置适配器的代码

addExpTypeAdapter = new AddExpTypeAdapter(AddExpTypeActivity.this, 
            R.layout.row_add_type, expTypeList, new ListItemBtnClickInterface() {

                private int clickedPosition;

                @Override
                public void listItemBtnClickListener(Object obj, int viewId, String value) {
                        // TODO Auto-generated method stub
                        clickedPosition = Integer.parseInt(obj.toString());

                        switch (viewId) {
                        case R.id.urView:
                            Log.d("Clicked TextView's Value ===> " , value);
                            break;

                        default:
                            break;
                        }
                    }
                });
        typeListView.setAdapter(addExpTypeAdapter);