在android中创建一个列表视图

在android中创建一个列表视图

本文介绍了在android中创建一个列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在android项目中制作listview时遇到问题。我的数据库有一个包含两列LocalID和LocalName的表。我使用listview在我的应用程序中显示LocalName。我的问题是,当我点击listview上的LocalName时,我可以获得此LocalName的LocalID。我不知道怎么做。请帮我。对不起我的英语,这不好。

I have a problem with to make a listview in android project. My database has a table with two columns "LocalID" and "LocalName". I use a listview to show "LocalName" in my app. My problem is i want to when I click a "LocalName" on listview I can get "LocalID" of this "LocalName". I don't know how to do it. Please help me. I sorry about my English, it is not good.

推荐答案


package com.mkyong.android;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ListFruitActivity extends ListActivity {

	static final String[] FRUITS = new String[] { "Apple", "Avocado", "Banana",
			"Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit",
			"Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple" };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// no more this
		// setContentView(R.layout.list_fruit);

		setListAdapter(new ArrayAdapter<string>(this, R.layout.list_fruit,FRUITS));

		ListView listView = getListView();
		listView.setTextFilterEnabled(true);

		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView					int position, long id) {
			    // When clicked, show a toast with the TextView text
			    Toast.makeText(getApplicationContext(),
				((TextView) view).getText(), Toast.LENGTH_SHORT).show();
			}
		});

	}
}</string>



查看更多 []!



-KR


See more here[^] !

-KR


这篇关于在android中创建一个列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:54