Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.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应用程序-如何显示项目列表并使其可点击_Android - Fatal编程技术网

Android应用程序-如何显示项目列表并使其可点击

Android应用程序-如何显示项目列表并使其可点击,android,Android,我需要在屏幕上显示一个文本项目列表,并使其可点击。因此,它类似于web应用程序上的链接列表 我如何在Android活动屏幕上做到这一点 这将是一些随机数目的项目,我必须从一个数据库,并显示为链接所有 你知道怎么做吗?你应该阅读有关的文档,然后按照说明进行操作。你应该使用列表视图。这很简单,只需创建一个ListActivity,将您的项目放入适配器中,然后将其设置为ListActivity的适配器 您可以阅读有关ListView的更多信息还有一个称为ListFragment的新范例 我以前使用过L

我需要在屏幕上显示一个文本项目列表,并使其可点击。因此,它类似于web应用程序上的链接列表

我如何在Android活动屏幕上做到这一点

这将是一些随机数目的项目,我必须从一个数据库,并显示为链接所有


你知道怎么做吗?

你应该阅读有关的文档,然后按照说明进行操作。

你应该使用
列表视图。这很简单,只需创建一个
ListActivity
,将您的项目放入
适配器中,然后将其设置为
ListActivity
适配器


您可以阅读有关ListView的更多信息

还有一个称为ListFragment的新范例

我以前使用过ListView,但现在更喜欢片段方法——它非常直接,非常灵活,尤其是在平板电脑上,因为在选择项目时与屏幕上另一个区域的交互非常灵活,只需要很少的代码

举个例子:

public class Select_FoodCategories_Fragment extends android.app.ListFragment {
    private static final boolean DEBUG = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    if (DEBUG)
        Log.i(this.getClass().getSimpleName(), " ->"
            + Thread.currentThread().getStackTrace()[2].getMethodName());
    super.onCreate(savedInstanceState);

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (DEBUG)
        Log.i(this.getClass().getSimpleName(), " ->"
            + Thread.currentThread().getStackTrace()[2].getMethodName());
    HoldingActivity a = (HoldingActivity) getActivity();
    //accessing a variable of the activity is easy
    a.visibleListViewInFragment = getListView();

    List<XYZ> listTodisplay = a.getListToDisplay();

    MyAdapter adapter = new MyAdapter(
        getActivity(), 0, listTodisplay);
    setListAdapter(adapter);

    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
    if (DEBUG)
        Log.i(this.getClass().getSimpleName(), " ->"
            + Thread.currentThread().getStackTrace()[2].getMethodName());
        XYZ item = (XYZ) getListAdapter()
        .getItem(position);

    }

}
public类Select\u FoodCategories\u Fragment扩展了android.app.listframent{
私有静态最终布尔调试=true;
@凌驾
创建时的公共void(Bundle savedInstanceState){
如果(调试)
Log.i(this.getClass().getSimpleName(),“->”
+Thread.currentThread().getStackTrace()[2].getMethodName());
super.onCreate(savedInstanceState);
}
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
如果(调试)
Log.i(this.getClass().getSimpleName(),“->”
+Thread.currentThread().getStackTrace()[2].getMethodName());
HoldingActivity a=(HoldingActivity)getActivity();
//访问活动的变量很容易
a、 visibleListViewInFragment=getListView();
List listTodisplay=a.getListToDisplay();
MyAdapter=新的MyAdapter(
getActivity(),0,listTodisplay);
setListAdapter(适配器);
}
@凌驾
public void onListItemClick(列表视图l、视图v、整数位置、长id){
如果(调试)
Log.i(this.getClass().getSimpleName(),“->”
+Thread.currentThread().getStackTrace()[2].getMethodName());
XYZ项=(XYZ)getListAdapter()
.getItem(职位);
}
}
更多信息请点击此处:

顺便说一句,我发现熟悉新的碎片概念是非常值得的——它只会让生活更轻松——尤其是在平板电脑上


ps我故意留下了调试语句,因为根据我的经验,这有助于alto更快地理解整个概念

是的,你可以做到。创建一个DataExchange类以从Db获取它。。 将字符串存储在数组中

创建ArrayAdapter以显示从数据库获得的字符串数组

比如说

public class AndroidListViewActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // storing string resources into Array
    String[] numbers = {"one","two","three","four"}
     // here you store the array of string you got from the database

    // Binding Array to ListAdapter
    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label,       numbers));
    // refer the ArrayAdapter Document in developer.android.com
    ListView lv = getListView();

    // listening to single list item on click
    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {

          // selected item 
          String num = ((TextView) view).getText().toString();

          // Launching new Activity on selecting single List Item
          Intent i = new Intent(getApplicationContext(), SingleListItem.class);
          // sending data to new activity
          i.putExtra("number", num);
          startActivity(i);

      }
    });
}
}
您必须相应地创建各种布局文件。。 希望这对你有帮助:)

public class SingleListItem extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.single_list_item_view);

    TextView txtProduct = (TextView) findViewById(R.id.product_label);

    Intent i = getIntent();
    // getting attached intent data
    String product = i.getStringExtra("number");
    // displaying selected product name
    txtProduct.setText(product);

}
}