[英]add icons in dynamic list view using android
我是android的新手,正在使用来自数据库的列表视图。 现在,我想在其中添加两个图标,一个用于编辑,一个用于删除。 这是我目前正在工作的Java代码
public void ListDrwaer() {
List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("emp_info");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("cat_name");
number = jsonChildNode.optString("cat_id");
String outPut = name /*+ "-" + number*/;
employeeList.add(createEmployee("employees", outPut));
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error" + e.toString(),
Toast.LENGTH_SHORT).show();
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList,
android.R.layout.simple_list_item_1,
new String[] { "employees" }, new int[] { android.R.id.text1 });
listView.setAdapter(simpleAdapter);
}
private HashMap<String, String> createEmployee(String name, String number) {
HashMap<String, String> employeeNameNo = new HashMap<String, String>();
employeeNameNo.put(name, number);
return employeeNameNo;
}
谁能告诉我如何编辑它以添加图标?
如您所说,您是新手,所以我正在创建一个与您的需求类似的列表视图。 如果您仍然遇到任何问题,可以提出疑问。 也可以有其他方法,但我遵循以下一种方法。
我通过三个步骤创建了它:1.用XML创建一个listview布局。 2.为您的行创建一个布局,将其充气并设置在listview行上。 3.通过扩展arrayadapter创建一个自定义适配器。 4.设置自定义适配器。
步骤1:使用XML创建一个listview布局。
下面给出的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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
步骤2:为您的行创建一个布局,然后将其充气并设置在listview行上。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="false"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="TextView" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="34dp"
android:layout_toRightOf="@+id/textView1"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/imageButton1"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
步骤3:通过扩展arrayadapter创建自定义适配器。
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class my_list_adapter extends ArrayAdapter<String> {
private Context context;
private ArrayList<String> labels;
public my_list_adapter(Context context, ArrayList<String> labels) {
super(context, R.layout.list_layout, labels);
this.context = context;
this.labels = labels;
}
static class ViewHolder {
public TextView textView1;
public ImageButton icon_1;
public ImageButton icon_2;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
rowView = inflater.inflate(R.layout.list_layout, null, true);
holder = new ViewHolder();
holder.textView1 = (TextView) rowView.findViewById(R.id.textView1);
holder.icon_1 = (ImageButton) rowView.findViewById(R.id.imageButton1);
holder.icon_2 = (ImageButton) rowView.findViewById(R.id.imageButton2);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
holder.textView1.setText(labels.get(position));
holder.icon_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context.getApplicationContext(), "bb icon 1 item clicked position = " + position, Toast.LENGTH_LONG).show();;
}
});
holder.icon_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context.getApplicationContext(), "bb icon 2 item clicked position = " + position, Toast.LENGTH_LONG).show();;
}
});
return rowView;
}
}
步骤4:设置自定义适配器。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list_view = (ListView) findViewById(R.id.listView1);
ArrayList<String> labels = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
labels.add ("item " + i);
}
my_list_adapter adapter = new my_list_adapter(this, labels);
list_view.setAdapter(adapter);
}
我认为以上示例可以为您提供帮助。 尝试理解这个概念。 如果您遇到任何问题,可以询问。 我会尽力帮助您。
使用CustomArray适配器添加编辑/删除按钮。 本教程将为您提供帮助。 请检查一下。
对于这种情况,您必须使用您的自定义适配器
表示扩展BaseAdapter
并尝试在该适配器中实现所有方法。
这些教程都不会对您有所帮助 ,因为没有一个教程会显示如何在自定义ArrayAdapter的getView()方法中将侦听器放在列表视图中的按钮上,以及如何确定listviev中的哪个项目拥有该按钮。 即使有办法做到这一点,您也必须弄清楚如何(从ArrayAdapter类)在Activity或Fragment中调用方法,然后可以在该方法上执行该操作。
因此,这为您提供了几种选择。 您需要响应列表项本身上的单击/按下,而不是按钮。 这是通过将OnItemClickListener或OnItemLongClickListener添加到列表视图来完成的。 然后,您可以通过以下几种方式对这些操作做出响应:
由于您将需要编写一个用于编辑该项目的对话框,因此我将使用“对话框”选项。
如果您是Android的新手,这会令人感到困惑,但是学习如何做是没有捷径的。 也尝试一下本教程,我发现它很有帮助:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.