如何在listview中使用onitemclick从mysql获取数据?

g9icjywg  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(214)

我有一个自定义的listview和一个从mysql获取数据的textview。单击项目时,我希望从textview获取的内容。我试过这个,

mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String value = mList.getItemAtPosition(position).toString();
        Toast.makeText(getBaseContext(), value, Toast.LENGTH_LONG).show();
    }
});

但它又回来了 com.wanto.sispaktriwanto.init.Penyakit@c05cb5c .
有人能帮我解决这个问题吗?谢谢。
这是型号代码:

package com.wanto.sispaktriwanto.init;    

public class Penyakit {

    private String penyakit_nama;

    public String getpenyakit_nama() {
        return penyakit_nama;
    }
    public void setpenyakit_nama(String penyakit_nama) {

        this.penyakit_nama = penyakit_nama;
    }
}

以下是从mysql获取数据的代码:

package com.wanto.sispaktriwanto.server;    

import com.wanto.sispaktriwanto.init.HasilKonsul;
import com.wanto.sispaktriwanto.init.Penyakit;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class GetDetailPenyakit {

    public static ArrayList<Penyakit> getDetail(String postvariable) {
        String detail = "";
        ArrayList<Penyakit> MyArraylist = new ArrayList<>();
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://10.0.2.2/android_sispak/getDetailPenyakit.php");
        try {
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            nameValuePairs.add(new BasicNameValuePair("detail", postvariable));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            detail = EntityUtils.toString(httpResponse.getEntity());

            JSONArray jsonArray = new JSONArray(detail);

            for (int i = 0; i < jsonArray.length(); i++) {
                Penyakit genres = new Penyakit();
                JSONObject MyJsonObject = jsonArray.getJSONObject(i);
                genres.setpenyakit_nama(MyJsonObject.getString("nama"));
                MyArraylist.add(genres);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return MyArraylist;
    }

}

这是适配器代码:

package com.wanto.sispaktriwanto.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.wanto.sispaktriwanto.R;
import com.wanto.sispaktriwanto.init.Penyakit;

import java.util.List;

public class PenyakitAdapter extends ArrayAdapter<Penyakit> {
    private final List<Penyakit> list;

    public PenyakitAdapter(Context context, int resource, List<Penyakit> list) {
        super(context, resource, list);
        this.list = list;
    }

    static class ViewHolder {
        protected TextView penyakitNama;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            LayoutInflater inflator = LayoutInflater.from(getContext());
            convertView = inflator.inflate(R.layout.row_penyakit, null);
            viewHolder = new ViewHolder();
            viewHolder.penyakitNama = (TextView) convertView.findViewById(R.id.row_namap);
            convertView.setTag(viewHolder);
            convertView.setTag(R.id.row_namap, viewHolder.penyakitNama);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.penyakitNama.setText(list.get(position).getpenyakit_nama());

        return convertView;
    }
}

这是活动代码:

package com.wanto.sispaktriwanto;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.wanto.sispaktriwanto.adapter.PenyakitAdapter;
import com.wanto.sispaktriwanto.init.Penyakit;
import com.wanto.sispaktriwanto.server.GetDetailPenyakit;
import com.wanto.sispaktriwanto.server.PenyakitJsonParser;

import java.util.ArrayList;

public class DetailPenyakit extends AppCompatActivity {

    Context context;
    ArrayList<Penyakit> array_list;
    GetDetailPenyakit JsonGetDetail;
    ListView mList;
    String konsultasiS;
    static String detailresponse;

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

        Intent intent = getIntent();
        detailresponse = intent.getExtras().getString("detail");

        context = this;
        new asyncTask_getPenyakit().execute();
    }

    public class asyncTask_getPenyakit extends AsyncTask<Void, Void, Void> {
        ProgressDialog dialog = new ProgressDialog(context);

        @Override
        protected void onPreExecute() {
            dialog.setTitle("Please wait...");
            dialog.setMessage("Loading Info Penyakit!");
            dialog.show();
            array_list = new ArrayList<>();

            JsonGetDetail = new GetDetailPenyakit();
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {
            array_list = JsonGetDetail.getDetail(detailresponse);
            return null;
        }

        protected void onPostExecute(Void s) {

            mList = (ListView) findViewById(R.id.detail_listView);
            final PenyakitAdapter detailAdapter = new PenyakitAdapter(context, R.layout.row_penyakit, array_list);
            mList.setAdapter(detailAdapter);
            mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position,
                                        long id) {
                    String value = mList.getItemAtPosition(position).toString();
                    Toast.makeText(getBaseContext(), value, Toast.LENGTH_LONG).show();
                }
            });

            super.onPostExecute(s);
            dialog.dismiss();
        }
    }
}

这是活动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"
    tools:context="com.wanto.sispaktriwanto.DetailPenyakit">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="100">

        <ListView
            android:id="@+id/detail_listView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:divider="@drawable/divider"
            android:dividerHeight="1sp"
            android:layout_weight="80"/>
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

和行xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:background="#ffffff"
    android:orientation="vertical">

    <TextView
        android:id="@+id/row_namap"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginLeft="20dp"
        android:maxLines="1"
        android:text="sss"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceMedium"/>    
</LinearLayout>
ux6nzvsh

ux6nzvsh1#

一个错误是,你的活动中已经有arraylist,所以试试这个,

mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position,
                                        long id) {
                                    String value =  array_list.get(position).getpenyakit_nama();

                    Toast.makeText(getBaseContext(), value, Toast.LENGTH_LONG).show();

                }
            });

一定要让我知道这是否有效

ebdffaop

ebdffaop2#

通过使用此代码,您可以获得textview的当前值。

mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position,
                                    long id) {
                                 String contactId = ((TextView) view.findViewById(R.id.row_namap)).getText().toString();

                Toast.makeText(getBaseContext(), value, Toast.LENGTH_LONG).show();

            }
        });
g6ll5ycj

g6ll5ycj3#

String value = mList.getItemAtPosition(position).toString();

我会得到的 Penyakit 对象。如果你想得到 penyakit_nama 值,使用下面的代码。

String value = ((Penyakit)mList.getItemAtPosition(position)).getpenyakit_nama();

因为arrayadapter的泛型类型是 <Penyakit> , getItemAtPosition() 会回来的 Penyakit 对象

相关问题