2013-04-22 106 views
1

我的ListView是从我的数据库中拉出的食谱列表。我正在尝试获取ListView中单击项目的文本。 ListView通过数据库调用和cursoradapter进行填充。我想使用所选项目的文本在另一个活动中进行另一个数据库调用。下面是代码块从点击列表视图项目获取文本

listView.setClickable(true); 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View v, int position, 
       long id) { 
      final String text = ((TextView)v).getText().toString(); 
      recipeid = myDBAdapter.getRecipeID(text); 
      Intent intent = new Intent(ListAllRecipes.this, DisplayRecipe.class); 
      intent.putExtra("recipeid", recipeid); 
      startActivity(intent); 

     } 

    }); 

当我运行我的代码,我得到

04-22 14:08:37.022: E/AndroidRuntime(25206): FATAL EXCEPTION: main 
04-22 14:08:37.022: E/AndroidRuntime(25206): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView 
04-22 14:08:37.022: E/AndroidRuntime(25206): at com.example.ketorecipes.ListAllRecipes$1.onItemClick(ListAllRecipes.java:47) 

当我点击在ListView的项目。

这里是一个大块的活动:

public class ListAllRecipes extends Activity{ 
private DBAdapter myDBAdapter; 
private ListView listView; 
private int recipeid; 

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



    myDBAdapter = new DBAdapter(this); 
    myDBAdapter.openToRead(); 
    Cursor c = myDBAdapter.getValues(); 

    listView = (ListView)findViewById(R.id.listView1); 
    String[] from = new String[] {"_id"}; 
    int[] to = new int[] {R.id.name_entry}; 
    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.list_entry, c, from, to); 
    listView.setAdapter(cursorAdapter); 



    listView.setClickable(true); 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View v, int position, 
       long id) { 
      final String text = ((TextView)v).getText().toString(); 
      recipeid = myDBAdapter.getRecipeID(text); 
      Intent intent = new Intent(ListAllRecipes.this, DisplayRecipe.class); 
      intent.putExtra("recipeid", recipeid); 
      startActivity(intent); 

     } 

    }); 
} 

}

+0

_id相反铸造底层布局'的名称'TextView''你应该使用''v.findViewById(your_textview_id)''检索文本。 – harism 2013-04-22 19:20:39

+0

谢谢你的回应。我对Android还是很陌生,我也不是一个非常强大的程序员。你能详细说明一下吗? – Michael 2013-04-22 19:32:48

+0

我假设你正在使用基于RelativeLayout的自定义布局来填充ListView?现在点击ListView将会给你这个布局主视图作为''View''参数。但是你可以通过使用View.findViewById(resource_id_assigned_for_TextView)来检索底层的''TextView''。 – harism 2013-04-22 19:37:52

回答

1

您将_id与R.id.name_entry关联,因此您的列表包含_id的任何值。

String[] from = new String[] {"_id"}; 
int[] to = new int[] {R.id.name_entry}; 

如果是这样,你可以得到它由

Cursor c = cursorAdapter.getCursor(); 
String text = c.getString(c.getColumnIndex("_id")); 
+0

谢谢!我成功地获取了字符串,现在我只需要弄清楚我搞砸了SQL的位置了! – Michael 2013-04-22 20:12:13

0

Im相当肯定的是,返回查看(V)不是一个TextView。 您的列表项目是自定义视图,因此您需要通过按id查找视图来获取视图。

1

我建议使用positiononItemClickparent.getItemAtPosition(position)获得该项目,这将返回表示您刚才点击的项目(可能需要转换为正确的类型),这应该是你想要的入口,无需手动从UI元素获取值。