gpt4 book ai didi

android - 如何在从mysql获取数据的listview中使用onItemClick?

转载 作者:行者123 更新时间:2023-11-29 07:31:51 25 4
gpt4 key购买 nike

我有一个自定义的 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;
}
}

这是 Activity 代码:

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

这是 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>

和行 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>

最佳答案

通过使用这段代码,您可以获得 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();

}
});

关于android - 如何在从mysql获取数据的listview中使用onItemClick?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50868540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com