【发布时间】:2013-12-20 22:06:49
【问题描述】:
我正在尝试为每个 ListView 条目链接一个编辑按钮,以编辑与该按钮关联的特定条目。例如,对于填充一行的每个数据库条目,最右侧将有一个按钮允许您编辑条目。目前,代码使用 onItemClickListener 来选择整行本身,但我想将其链接到行中的按钮,而不是必须选择整行。
这是我的 MainActivity.java 代码:
package com.example.project_input;
import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.app.ListActivity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.ListView;
public class MainActivity extends ListActivity {
Intent intent;
TextView projectId;
DBController controller = new DBController(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<HashMap<String, String>> projectList = controller.getAllProjects();
if(projectList.size()!=0) {
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
projectId = (TextView) view.findViewById(R.id.projectId);
String valProjectId = projectId.getText().toString();
Intent objIndent = new Intent(getApplicationContext(),EditProject.class);
objIndent.putExtra("projectId", valProjectId);
startActivity(objIndent);
}
});
ListAdapter adapter = new SimpleAdapter( MainActivity.this,projectList, R.layout.view_project_entry, new String[] { "projectId","projectName", "projectLocation"}, new int[] {R.id.projectId, R.id.projectName, R.id.projectLocation});
setListAdapter(adapter);
}
}
public void showAddForm(View view) {
Intent objIntent = new Intent(getApplicationContext(), NewProject.class);
startActivity(objIntent);
}
}
按钮的布局xml如下:
<Button
android:id="@+id/edit_button"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/add_details"
android:paddingRight=" 10dp"
android:text="Edit" />
对此的任何帮助将不胜感激。
【问题讨论】:
标签: android listview button onclicklistener