【问题标题】:android onClickListener with a bugandroid onClickListener 有一个错误
【发布时间】:2014-10-21 20:12:46
【问题描述】:

我正在尝试实现 onItemClickListener,附件是我的代码和 xml 文件。据我了解,当我单击任何 textview 时,我应该返回唯一 ID;但在这里它只返回 0 。不知道为什么,提前感谢您的解决方案。

我的主要活动:

public class MainActivity extends ListActivity implements OnItemClickListener   {
public static final String number = "number" ;
public static final String email ="email" ;
public static final String name ="name";
ArrayList<HashMap<String, String>> userList = new ArrayList<HashMap<String,String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
for (int i=0 ;i <=5;i++){
    HashMap<String, String> map = new HashMap<String, String>();
    map.put(number, i+"");
    map.put(email, i+"email");
    map.put(name,i+"name");

    userList.add(map);  
}
settextView(userList);
final ListView lv = getListView();
lv.setTextFilterEnabled(false);
lv.setOnItemClickListener(this);
}

public void settextView(ArrayList<HashMap<String, String>> userList) 
{
 ListAdapter adapter = new SimpleAdapter(
            MainActivity.this, userList,
            R.layout.list_item, new String[] {number , email ,name  },
            new int[] { R.id.userName, R.id.ventDesc ,R.id.ventUserName});
    // updating listview
    setListAdapter(adapter);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub

String numberPressed = ((TextView)findViewById(R.id.userName)).getText().toString();
Toast.makeText(MainActivity.this, "CLICK: " + numberPressed, Toast.LENGTH_SHORT).show();
Log.d("selected is ",numberPressed);

}
}

主xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="20sp" >
</TextView>

列表 xml:

   <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >

    <TextView
    android:id="@+id/userName"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

     />
<!-- Name Label -->

    <TextView
    android:id="@+id/ventUserName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

 <TextView
    android:id="@+id/ventDesc"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="6dip"
    android:paddingTop="6dip"
    android:textSize="17dip"
    android:textStyle="bold" />

    </LinearLayout>

PS:我确实尝试过调试代码,老实说似乎没有任何帮助

【问题讨论】:

  • 您希望它返回什么“唯一 ID”,为什么?
  • 可能是 String numberPressed = ((TextView) view.findViewById(R.id.userName)).getText().toString();
  • @codeMagic 我正在尝试打印“用户名”
  • @Deucalion --> 谢谢它起作用了,我现在明白了:)

标签: android android-listview onitemclicklistener


【解决方案1】:

更改字符串 numberPressed = ((TextView)findViewById(R.id.userName)).getText().toString(); to String numberPressed = ((TextView)view.findViewById(R.id.userName)).getText().toString();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多