简体   繁体   English

使用Android在动态列表视图中添加图标

[英]add icons in dynamic list view using android

I am new in android and i am using list view that is coming from the database . 我是android的新手,正在使用来自数据库的列表视图。 Now , I want to add two icons in it , one for edit and one for delete. 现在,我想在其中添加两个图标,一个用于编辑,一个用于删除。 Here is my java code that is currently working 这是我目前正在工作的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;
     }

can anyone tell me how to edit it to add the icons ? 谁能告诉我如何编辑它以添加图标?

As you told you are new so I am creating a listview similar to your needs. 如您所说,您是新手,所以我正在创建一个与您的需求类似的列表视图。 If you are still facing any problem then you can ask. 如果您仍然遇到任何问题,可以提出疑问。 There can be other methods also but I have followed below one. 也可以有其他方法,但我遵循以下一种方法。

I have created it in three steps: 1. Create a listview layout in XML. 我通过三个步骤创建了它:1.用XML创建一个listview布局。 2. Create a layout for your row which you will inflate and set on listview row. 2.为您的行创建一个布局,将其充气并设置在listview行上。 3. Create a custom adapter by extending arrayadapter. 3.通过扩展arrayadapter创建一个自定义适配器。 4. Setting custom adapter. 4.设置自定义适配器。

Step 1: Create a listview layout in XML. 步骤1:使用XML创建一个listview布局。

Below given XML code will create a listview for you. 下面给出的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>

?Step 2: Create a layout for your row which you will inflate and set on listview row. 步骤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>

Step 3:Create a custom adapter by extending arrayadapter. 步骤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;
    }
}

Step 4: Setting custom adapter. 步骤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);
    }

I think above example can help you. 我认为以上示例可以为您提供帮助。 Try to get the concept. 尝试理解这个概念。 If you are facing any problem then you can ask. 如果您遇到任何问题,可以询问。 I will try to help you. 我会尽力帮助您。

Use CustomArray adapter to add edit/delete button. 使用CustomArray适配器添加编辑/删除按钮。 This tutorial will help you. 教程将为您提供帮助。 please check it. 请检查一下。

For this scenario you have to use ur custom adapter 对于这种情况,您必须使用您的自定义适配器

means extends BaseAdapter 表示扩展BaseAdapter

and try to implement all methods in that adapter. 并尝试在该适配器中实现所有方法。

None of the tutorials are going to help you , because none show how to put a listener on a button in a listview in the getView() method of a custom ArrayAdapter and show you how to figure out what item in the listviev owns that button. 这些教程都不会对您有所帮助 ,因为没有一个教程会显示如何在自定义ArrayAdapter的getView()方法中将侦听器放在列表视图中的按钮上,以及如何确定listviev中的哪个项目拥有该按钮。 Even if there is a way to do this, you then have to figure out how (from the ArrayAdapter class) to call a method back in your Activity or Fragment, where you can then act on that item. 即使有办法做到这一点,您也必须弄清楚如何(从ArrayAdapter类)在Activity或Fragment中调用方法,然后可以在该方法上执行该操作。

So, this leaves you with several options. 因此,这为您提供了几种选择。 Instead of buttons, you need to respond to clicks/presses on the list item itself. 您需要响应列表项本身上的单击/按下,而不是按钮。 This is done by adding an OnItemClickListener or OnItemLongClickListener to the listview. 这是通过将OnItemClickListener或OnItemLongClickListener添加到列表视图来完成的。 You can then respond to these actions in several ways: 然后,您可以通过以下几种方式对这些操作做出响应:

  • A context menu, where the user can select the option to delete or edit the item (using a listview of menu options). 上下文菜单,用户可以在其中选择删除或编辑项目的选项(使用菜单选项的列表视图)。
  • A pop-up Dialog that does the same thing with buttons. 一个弹出对话框,使用按钮执行相同的操作。 Ot it could put them in edit mode and have a delete button too. 可能会使它们进入编辑模式,并且还有删除按钮。

Since you are going to need to write a Dialog for the editing of the item, I would go with the Dialog option. 由于您将需要编写一个用于编辑该项目的对话框,因此我将使用“对话框”选项。

It is confusing if you're new to Android, but there's no shortcut in learning how to do it. 如果您是Android的新手,这会令人感到困惑,但是学习如何做是没有捷径的。 Try this tutorial as well, I found it pretty helpful: 也尝试一下本教程,我发现它很有帮助:

http://theopentutorials.com/tutorials/android/listview/android-custom-listview-with-image-and-text-using-baseadapter http://theopentutorials.com/tutorials/android/listview/android-custom-listview-with-image-and-text-using-baseadapter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM