如何从listview.setadapter(适配器)获取值?

时间:2014-07-09 06:55:46

标签: android database sqlite listview android-listview

大家晚上好。

首先,我有两个使用相同代码的活动。在1个活动中,此代码非常有效。我在listview中得到了值。顺便说一下,这个活动使用listview来引用RecipesDataSourceAdapter,RecipesDataSource和RecipesDataSourceContent(这不是SQLite数据库引用)。在第二个活动中,当我单击列表视图以获取值时,这会给我一个错误。顺便说一下,这个活动使用sqlite数据库作为参考。

此外,我尝试使用:扩展ListActivity,扩展Activity实现onClickListener,并扩展ListActivity实现onClickListener,在所有我得到相同的错误。

这是代码:

private YAODeckListDataSource datasource;

private ListView recipesListView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
      setContentView(R.layout.frmdeckedithome);

   copydbfromassest();



    datasource = new YAODeckListDataSource(this);
    datasource.open();


      List<YAODeckList> values = datasource.getAllComments();  

      ArrayAdapter<YAODeckList> adapter = new ArrayAdapter<YAODeckList>(this,
      android.R.layout.simple_list_item_1, values);


      recipesListView = (ListView)findViewById(R.id.lstcard);  

      recipesListView.setAdapter(adapter);

    recipesListView.setTextFilterEnabled(true);
        recipesListView.setClickable(true);
        recipesListView.setOnItemClickListener(new OnItemClickListener()
        {

            public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
            {


                // ListView Clicked item index (the position)
                 int itemPosition     = position;

                 // ListView Clicked item text (the text displayed)

                 TextView name;
                 name = (TextView)v.findViewById(R.list.text);
                 String message = name.getText().toString();  /// <<<<<< here is where i get an error
                 //displaying the information in popmessage
                    AlertDialog.Builder adb = new AlertDialog.Builder(YAOhomedeckedit.this);
                    adb.setTitle("ListView OnClick");
                    adb.setMessage("Selected Item is = "+ (position + 1)+ " of " + recipesListView.getCount() +"  "+ 
                            "Position :"+itemPosition+"  ListItem Text : " + message );

                    adb.setPositiveButton("Ok", null);
                    adb.show();

            }
        });
} 

好处是列表视图会填充相应的信息,但是当我点击列表项时,我会收到错误。

      07-09 06:23:00.319: E/AndroidRuntime(500): FATAL EXCEPTION: main
      07-09 06:23:00.319: E/AndroidRuntime(500): java.lang.NullPointerException
      07-09 06:23:00.319: E/AndroidRuntime(500):    at com.example.yao.YAOhomedeckedit$1.onItemClick(YAOhomedeckedit.java:129)
      07-09 06:23:00.319: E/AndroidRuntime(500):    at android.widget.AdapterView.performItemClick(AdapterView.java:284)
      07-09 06:23:00.319: E/AndroidRuntime(500):    at android.widget.ListView.performItemClick(ListView.java:3382)
      07-09 06:23:00.319: E/AndroidRuntime(500):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
      07-09 06:23:00.319: E/AndroidRuntime(500):    at android.os.Handler.handleCallback(Handler.java:587)
      07-09 06:23:00.319: E/AndroidRuntime(500):    at android.os.Handler.dispatchMessage(Handler.java:92)
      07-09 06:23:00.319: E/AndroidRuntime(500):    at android.os.Looper.loop(Looper.java:123)
      07-09 06:23:00.319: E/AndroidRuntime(500):    at android.app.ActivityThread.main(ActivityThread.java:4627)
      07-09 06:23:00.319: E/AndroidRuntime(500):    at java.lang.reflect.Method.invokeNative(Native Method)
      07-09 06:23:00.319: E/AndroidRuntime(500):    at java.lang.reflect.Method.invoke(Method.java:521)
      07-09 06:23:00.319: E/AndroidRuntime(500):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
      07-09 06:23:00.319: E/AndroidRuntime(500):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
      07-09 06:23:00.319: E/AndroidRuntime(500):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

Yout name textview null ,因为它不属于View v。您可以直接将View v投射到Textview作为{{1}因为你不需要再次创建视图。

即。在您的代码中,获取消息

((TextView) v).getText()

而不是

String message =  ((TextView) v).getText().toString();

答案 1 :(得分:0)

由于以下行

,您获得了NullPointerException
name = (TextView)v.findViewById(R.list.text);

您正在android.R.layout.simple_list_item_1中传递布局adapter,而您在上面的行中没有TextView ID为text

查看android.R.layout.simple_list_item_1

的源代码

所以你可以使用

name = (TextView)v.findViewById(android.R.id.text1);

name = ((TextView) v);

因为布局只包含一个TextView

答案 2 :(得分:0)

public View findViewById (int id)
// Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle).
// Returns The view if found or null otherwise.

在您的代码中,

name = (TextView)v.findViewById(R.list.text);

上述行错了。您必须发送要访问的视图的id属性。