意向中的ListView(Intent in ListView)

2019-11-04 01:39发布

如何点击一个ListView。 我的意思是如何使意图就可以了。

JAVA

public class Login extends ActionBarActivity {
    ListView listView;
    ArrayAdapter<String> adapter;
    String[] grocery_categories = {"Beverages", "Bakery", "Canned Goods", "Condiments", "Dairy", "Snacks", "Frozen Foods",
                                    "Meat", "Produce", "Cleaners", "Paper Goods", "Personal Care", "Others"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        listView = (ListView) findViewById(R.id.list_view);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, grocery_categories);
        listView.setAdapter(adapter);    
    }
}

XMS

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.admin.mobilegroceryapp.Login"
android:id="@+id/rl_login">

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/login_bckgrnd"
    android:src="@drawable/login_bckgrnd"
    android:scaleType="centerCrop"
    />

<ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">



</ListView>

我的方式和探索Android Studio中我的论文的学生。

Answer 1:

尝试

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
            String grocery = (String) listView.getAdapter().getItem(position);
            Intent intent = new Intent(listView.getContext(),/*Your activity*/);
            listView.getContext().startActivity(intent);
            //or create other intents here
        }
    });

现在,你的代码应该是这样的:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
listView = (ListView) findViewById(R.id.list_view);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, grocery_categories);
listView.setAdapter(adapter);
  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
            String grocery = (String) listView.getAdapter().getItem(position);
            Intent intent = new Intent(listView.getContext(),/*Your activity*/);
            listView.getContext().startActivity(intent);
            //or create other intents here
        }
    });

}


Answer 2:

在itemClick在监听器设置为你的列表视图,您可以探索更多的ListView这里http://developer.android.com/guide/topics/ui/layout/listview.html如果你想在列表视图中点击使用意图只是把你的代码的ListView点击收听



Answer 3:

您可以使用OnItemClickListener ,以确定哪些列表项被点击。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String itemChosen = (String) parent.getItemAtPosition(position);
        Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
        intent.putExtra("groceryItem", itemChosen);
        startActivity(intent);
    }
}


文章来源: Intent in ListView