【问题标题】:Button in ListView to edit sqlite database entryListView 中用于编辑 sqlite 数据库条目的按钮
【发布时间】: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


    【解决方案1】:

    为了在ListView 的一行中创建Button 可点击,您需要在Button 本身上设置onClickListener。由于在行项目上具有Button 不是Android 附带的东西,因此您需要使用Button 创建自己的行。最简单的方法是为您的行创建一个 .xml,对其进行膨胀并获取对您的 AdaptergetView() 中的按钮的引用。

    【讨论】:

      猜你喜欢
      • 2013-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多