我正在开发一个Android项目,该项目有几个屏幕,每行最多可以点击200行。我想弄清楚的问题是如何在不添加200行
的情况下使它们全部被点击TableRow r1 = (TableRow) findViewById(R.id.table_row_1);
TableRow r2 = (TableRow) findViewById(R.id.table_row_2);
TableRow r3 = (TableRow) findViewById(R.id.table_row_3);
TableRow r4 = (TableRow) findViewById(R.id.table_row_4);
r1.setOnClickListener(listener);
r2.setOnClickListener(listener);
r3.setOnClickListener(listener);
r4.setOnClickListener(listener);
最终行将获取它们的id并在数据库中搜索值(我将使用每个表行作为数据库中值的一个键来填充行中的列)但是现在我是只需尝试在单击每个行时更改行的背景颜色。
问题: 如何在没有数千行冗余代码的情况下处理大量可点击的行?我是否需要为每一行设置一个OnClickListener,或者是否有一个更好的方法,我正在看?有没有办法在XML中做到这一点?
答案 0 :(得分:2)
使用ListView
与自定义ListAdapter
MainActivity.java:
public class MainActivity extends ListActivity implements AdapterView.OnItemClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpComponents();
}
private void setUpComponents(){
ArrayList<String> myValuesToDisplay = getDatabaseContent();
MyCustomListAdapter adapter = new MyCustomListAdapter(this, myValuesToDisplay);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
private ArrayList<String> getDatabaseContent(){
/*
This is where you would like to connect to your database and fetch the content.
In this example, we simulate the result by returning an ArrayList<String>
*/
ArrayList<String> values = new ArrayList<String>();
values.add("Value1");
values.add("Value2");
values.add("Value3");
return values;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//When you click on an item in the list view, you fetch the position in the list
System.out.println("Clicked on item with position: " + position);
}
}
MyCustomListAdapter.java:
public class MyCustomListAdapter extends ArrayAdapter<String> {
private ArrayList<String> yourArray;
public MyCustomListAdapter(Context ctx, ArrayList<String> yourArray){
super(ctx, R.layout.my_custom_list_item, yourArray);
this.yourArray = yourArray;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Re-use rows to save battery
View row;
if (convertView == null) {
//We inflate our custom view for the ListView item
LayoutInflater inflater = LayoutInflater.from(getContext());
row = inflater.inflate(
R.layout.my_custom_list_item, null);
} else {
row = convertView;
}
//Get the current item of the array
String arrayItem = yourArray.get(position);
//Get the text view in the layout of which we want to display the value
TextView tvListItem = (TextView) row.findViewById(R.id.tv_list_item);
//Set the text
tvListItem.setText(arrayItem);
//Return the row to the ListView
return row;
}
}
ActivityMain.xml:
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/list" />
</RelativeLayout>
my_custom_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/tv_list_item" />
</LinearLayout>
此解决方案将创建一个可滚动的ListView
并使用您的数据库值填充它。您对ListAdapter
的实施可能会有所不同。您可以通过更改my_custom_list_item.xml
中的布局来选择要显示的内容和方式。
结果:
单击一行将打印出其在列表中的位置。例如,您可以使用该信息启动另一个活动,显示有关该条目的详细信息。