Как добавить Button в Custom Listview - PullRequest
0 голосов
/ 10 апреля 2020

я знаю, что есть много вопросов и тем об этом, но я пытаюсь это исправить в течение двух дней. Ни один из них не помог мне.

У меня есть просмотр списка, который я получаю из файла xml с парсером xml. (я использовал этот урок (https://www.tutlane.com/tutorial/android/android-xml-parsing-using-sax-parser))

Мне нужно добавить кнопку в каждую строку, я пробовал каждое решение на inte rnet, но будьте c, я новичок ie, я не смог добиться успеха.

У меня есть песнопения. java

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.thekhaeng.pushdownanim.PushDownAnim;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

public class Chants extends AppCompatActivity {
    private static final int MANTRA_DETAILS_ACTIVITY_REQUEST_CODE = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview);

        overridePendingTransition(R.anim.fade_in_layout,R.anim.fade_out_layout);



        try{
            ArrayList<HashMap<String, String>> userList = new ArrayList<>();
            HashMap<String,String> user = new HashMap<>();
            ListView lv = (ListView) findViewById(R.id.user_list);
            InputStream istream = getAssets().open("userdetails.xml");
            XmlPullParserFactory parserFactory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = parserFactory.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,false);
            parser.setInput(istream,null);
            String tag = "" , text = "";
            int event = parser.getEventType();
            while (event!= XmlPullParser.END_DOCUMENT){
                tag = parser.getName();
                switch (event){
                    case XmlPullParser.START_TAG:
                        if(tag.equals("user"))
                            user = new HashMap<>();
                        break;
                    case XmlPullParser.TEXT:
                        text=parser.getText();
                        break;
                    case XmlPullParser.END_TAG:
                        switch (tag){
                            case "name": user.put("name",text);
                                break;
                            case "designation": user.put("designation",text);
                                break;
                            case "location": user.put("location",text);
                                break;
                            case "user":
                                if(user!=null)
                                    userList.add(user);
                                break;
                        }
                        break;
                }
                event = parser.next();
            }
            ListAdapter adapter = new SimpleAdapter(Chants.this, userList, R.layout.list_row,new String[]{"name","designation","location"}, new int[]{R.id.name, R.id.designation, R.id.location});
            lv.setAdapter(adapter);

           /// lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
               /// public void onItemClick(AdapterView<?> parent, View view,
                 ///                       int position, long id) {
                ///    TextView textView = (TextView) ////view.findViewById(R.id.name);
                ///    String text = textView.getText().toString();
                ///    System.out.println("Choosen Country = : " + text);
///
             ///   }});


        }
        catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }


    }


}

и активность_список

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/user_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="1dp" />

</LinearLayout>

и список_объектов. xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/alternate_orange"
    android:clickable="true"
    android:focusable="true"
    tools:context=".Chants">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@color/color1"
        android:gravity="fill_horizontal|center"
        tools:ignore="MissingConstraints">


        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_alignParentStart="true"
            android:layout_marginStart="85dp"
            android:gravity="fill_vertical"
            android:text=""
            android:theme="@style/MantraEditText"
            android:textColor="#78038A"
            android:textSize="18.5dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/designation"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_alignStart="@+id/name"
            android:layout_below="@+id/name"
            android:fontFamily="serif"
            android:gravity="fill_vertical"
            android:text=""
            android:textColor="#000000"
            android:textSize="16dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/location"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_alignStart="@+id/designation"
            android:layout_below="@+id/designation"
            android:fontFamily="serif"
            android:gravity="fill_vertical"
            android:text=""
            android:textColor="#000000"
            android:textSize="16dp"
            android:textStyle="bold" />



    </RelativeLayout>

    </android.support.constraint.ConstraintLayout>

Итак, я хочу добавить кнопку отправки в list_row. xml как я могу это сделать?

Любая помощь будет высоко оценена, спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...