Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从listview.setadapter(适配器)获取值?_Android_Database_Sqlite_Listview_Android Listview - Fatal编程技术网

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

Android 如何从listview.setadapter(适配器)获取值?,android,database,sqlite,listview,android-listview,Android,Database,Sqlite,Listview,Android Listview,各位晚上好 首先,我有两个活动使用同一段代码。在活动1中,这段代码非常有效。我在listview中获取值。顺便说一句,此活动使用listview引用RecipesDataSourceAdapter、RecipesDataSource和RecipesDataSourceContent(这不是SQLite数据库引用)。在第二个活动中,当我单击listview获取值时,会出现一个错误。顺便说一句,此活动使用sqlite数据库作为参考 此外,我还尝试使用了扩展ListActivity、扩展Activit

各位晚上好

首先,我有两个活动使用同一段代码。在活动1中,这段代码非常有效。我在listview中获取值。顺便说一句,此活动使用listview引用RecipesDataSourceAdapter、RecipesDataSource和RecipesDataSourceContent(这不是SQLite数据库引用)。在第二个活动中,当我单击listview获取值时,会出现一个错误。顺便说一句,此活动使用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();

            }
        });
} 

您的
name textview
null,因为它不是
View v
的一部分。您可以将
View v
直接转换为
textview
((textview)v)。getText(),因为您不需要再次创建视图

i、 e.在您的代码中,获取消息作为

String message =  ((TextView) v).getText().toString();
而不是

TextView name;
name = (TextView)v.findViewById(R.list.text);
String message = name.getText().toString(); 

由于以下行,您将获得
NullPointerException

name = (TextView)v.findViewById(R.list.text);
您正在传递
adapter
中的布局
android.R.layout.simple\u list\u item\u 1
,它没有您在上面一行中尝试执行的id为
text
TextView

请查看源代码以获取更多信息

所以你可以使用

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

因为版面只包含一个
TextView

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属性。

我想name对象是空的,最好检查一下id,这样它就可以解决您的问题。yahomedecedit.java第129行,错误:NullPointerException,修复它谢谢,你们帮了大忙
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);