我是Stack Overflow的新手,我读了这个帖子(ListView with Add and Delete Buttons in each Row in android),我在我的代码中实现了答案(https://stackoverflow.com/a/23021960/7262380)。一切正常。但是现在我想要,如果我点击列表视图项目,发生什么事情我必须做什么?
我的实际代码(列表适配器)
package de.control;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class listAdapter extends BaseAdapter implements ListAdapter {
//Create Variables
private ArrayList<String> list = new ArrayList<String>();
private ArrayList<Sort> aktuelleObjektListe = new ArrayList<>();
private Context context;
public static DBHelper dataSource;
TextView quantity = null;
public static final String LOG_TAG = listAdapter.class.getSimpleName();
//Finished
//Create ListAdapter
public listAdapter(ArrayList<String> list, Context context) {
this.list = list;
this.context = context;
dataSource = new DBHelper(context);
dataSource.open();
aktuelleObjektListe = dataSource.getAllSorts();
dataSource.close();
}
//Finished
//Unused but required
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int pos) {
return list.get(pos);
}
@Override
public long getItemId(int pos) {
return 0;
//just return 0 if your list items do not have an Id variable.
}
//Finished
//Update ListView ( is Called by "adapter.notifyDataSetChanged();" )
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.listlayout, null);
}
TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
listItemText.setText(list.get(position));
quantity = (TextView)view.findViewById(R.id.quantity_double);
quantity.setText( Integer.toString( aktuelleObjektListe.get(position).getQuantity() ) );
//Finished
//Handle ADD and DELETE Button
Button add1toSort = (Button)view.findViewById(R.id.add1toSort);
Button remove1fromSort = (Button)view.findViewById(R.id.remove1fromSort);
add1toSort.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
dataSource.open();
Sort sort = dataSource.getAllSorts().get(position);
dataSource.updateSortInDB(sort.getId(), sort.getSort(), (sort.getQuantity() + 1) );
aktuelleObjektListe = dataSource.getAllSorts();
dataSource.close();
notifyDataSetChanged();
}
});
remove1fromSort.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
dataSource.open();
Sort sort = dataSource.getAllSorts().get(position);
dataSource.updateSortInDB(sort.getId(), sort.getSort(), (sort.getQuantity() - 1) );
aktuelleObjektListe = dataSource.getAllSorts();
dataSource.close();
notifyDataSetChanged();
}
});
//Finished
return view;
}
}
我的实际代码(MainActivity)
package de.control;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
public class overview extends AppCompatActivity {
//Create Variable
public static DBHelper dataSource;
public static final String LOG_TAG = overview.class.getSimpleName();
public static final String tmp = "Test";
ListView possessionList;
Button addSort;
listAdapter adapter;
public ArrayList<String> listItems;
public static ArrayList<Sort> Objects;
//Finished
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.overview);
// Create ListView (ID: possessionList)
dataSource = new DBHelper(this);
dataSource.open();
Objects = dataSource.getAllSorts();
possessionList = (ListView)findViewById(R.id.overview_possessionList);
listItems = new ArrayList<String>();
adapter = new listAdapter(listItems, this);
possessionList.setAdapter(adapter);
Log.d(LOG_TAG, "Die Datenquelle wird geöffnet.");
Log.d(LOG_TAG, "ListView wird Aktuelisiert");
updateList();
Log.d(LOG_TAG, "Die Datenquelle wird geschlossen.");
dataSource.close();
//Finished
// Handle what happend by Pressing a List Item (DOESENT WORK)
possessionList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(LOG_TAG, "Test");
}
});
//Finished
// Switch Label on Button (ID: addSort)
addSort = (Button)findViewById(R.id.overview_completeAddition);
final Intent addIntent = new Intent(overview.this,addsort.class);
addSort.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
startActivity(addIntent);
}
});
//Finished
}
// Add all Objekts to the ListView
public boolean updateList()
{
try {
listItems.clear();
Objects = dataSource.getAllSorts();
for (int i = 0; i < Objects.size(); i++) {
listItems.add(Objects.get(i).getSort());
}
adapter.notifyDataSetChanged();
return true;
}
catch (Exception ex){
return false;
}
}
//Finished
//Add the Menü
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//Finished
}
答案 0 :(得分:5)
如何将setOnClickListener添加到我的ListView Android
您应该将setOnItemClickListener
实施到您的拥有列表listView
possessionList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// your code
}
});
另一种选择是在android:descendantFocusability="blocksDescendants"
或LinearLayout
,不在 listView元素中添加RelativeLayout
。
示例强>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
.....
android:descendantFocusability="blocksDescendants"
tools:context="de.control.overview">
答案 1 :(得分:0)
possessionList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i("Hello!", "Y u no see me?");
}
});
答案 2 :(得分:0)
最好使用recyclerview
代替listView
。
// Item Click Listener for the listview
OnItemClickListener itemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View container, int
position, long id) {
// Getting the Container Layout of the ListView
LinearLayout linearLayoutParent = (LinearLayout) container;
// Getting the inner Linear Layout
LinearLayout linearLayoutChild = (LinearLayout )
linearLayoutParent.getChildAt(1);
// Getting the Country TextView
TextView tvCountry = (TextView) linearLayoutChild.getChildAt(0);
Toast.makeText(getBaseContext(), tvCountry.getText().toString(),
Toast.LENGTH_SHORT).show();
}
};
// Setting the item click listener for the listview
listView.setOnItemClickListener(itemClickListener);
答案 3 :(得分:0)
例如,如果我有这样的列表视图:
ListView mListView = (ListView) mLayout.findViewById(R.id.my_list_view);
您可以使用setOnItemClickListener
点击列表视图事件中的每个项目:
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//Do what you want to each item here
}
});
更详细的示例:https://www.androidhive.info/2011/10/android-listview-tutorial/
答案 4 :(得分:0)
尝试将android:focusable="false"
添加到文字视图和按钮中
在您的listlayout.xml
元素中
在列表布局的父布局中添加此行android:descendantFocusability="blocksDescendants"
。