1

I have a custom ListView with a TextView that get data from mysql. What I want to get the content from textview when the item is clicked. I have tried this,

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();
    }
});

but it returns com.wanto.sispaktriwanto.init.Penyakit@c05cb5c.

Can anyone help me to solve this? Thanks.

This is model code:

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;
    }
} 

This is code for get data from 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;
    }

}

This is adapter code:

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;
    }
}

This is activity code:

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();
        }
    }
}

This is activity 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>

and row 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>

3 Answers 3

1

By using this code you can get the current value of the 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();

            }
        });
Sign up to request clarification or add additional context in comments.

Comments

0

one mistake is there, You already have arraylist in your activity so try this,

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();

                }
            });

Do let me know if this works or not

Comments

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

it will be get Penyakit object. if you want get penyakit_nama value, use below code.

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

because generic type of your ArrayAdapter is <Penyakit>, getItemAtPosition() will return Penyakit object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.