Java 在ListView中使用mySQL数据
三天来,我一直在尝试使用mySQL数据来实现我的ListView。我成功地使用php请求获得了数据,但我的应用程序需要一些时间才能从网络接收数据,这一事实存在一些问题。。。 所以,当我的listView应该实现时,arraylist是空的,并且它不工作 我尝试了一些解决方案,比如“AsyncTask”,但我不知道如何以我的方式使用它 我真希望有人能帮我,我迷路了 我的“repertoire.java”: My ContactAdapter.java:Java 在ListView中使用mySQL数据,java,php,android,mysql,android-asynctask,Java,Php,Android,Mysql,Android Asynctask,三天来,我一直在尝试使用mySQL数据来实现我的ListView。我成功地使用php请求获得了数据,但我的应用程序需要一些时间才能从网络接收数据,这一事实存在一些问题。。。 所以,当我的listView应该实现时,arraylist是空的,并且它不工作 我尝试了一些解决方案,比如“AsyncTask”,但我不知道如何以我的方式使用它 我真希望有人能帮我,我迷路了 我的“repertoire.java”: My ContactAdapter.java: package com.example.jo
package com.example.joignabilite;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ContactAdapter extends BaseAdapter {
// Une liste de Contacts
private List<Contact> mListP;
//Le contexte dans lequel est présent notre adapter
private Context mContext;
//Un mécanisme pour gérer l'affichage graphique depuis un layout XML
private LayoutInflater mInflater;
public ContactAdapter(Context context, List<Contact> aListP) {
mContext = context;
mListP = aListP;
mInflater = LayoutInflater.from(mContext);
}
public int getCount() {
return mListP.size();
}
public Object getItem(int position) {
return mListP.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout layoutItem;
//(1) : Réutilisation des layouts
if (convertView == null) {
//Initialisation de notre item à partir du layout XML "Contact_layout.xml"
layoutItem = (LinearLayout) mInflater.inflate(R.layout.item_contact, parent, false);
} else {
layoutItem = (LinearLayout) convertView;
}
//(2) : Récupération des TextView de notre layout
TextView tv_Nom = (TextView)layoutItem.findViewById(R.id.repertoire_item);
//(3) : Renseignement des valeurs
tv_Nom.setText(mListP.get(position).nom);
//On mémorise la position de la "Contact" dans le composant textview
tv_Nom.setTag(position);
//On ajoute un listener
tv_Nom.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Lorsque l'on clique sur le nom, on récupère la position de la "Contact"
Integer position = (Integer)v.getTag();
//On prévient les listeners qu'il y a eu un clic sur le TextView "TV_Nom".
sendListener(mListP.get(position), position);
}
});
//On retourne l'item créé.
return layoutItem;
}
/**
* Interface pour écouter les évènements sur le nom d'une Contact
*/
public interface ContactAdapterListener {
public void onClickNom(Contact item, int position);
}
//Contient la liste des listeners
private ArrayList<ContactAdapterListener> mListListener = new ArrayList<ContactAdapterListener>();
/**
* Pour ajouter un listener sur notre adapter
*/
public void addListener(ContactAdapterListener aListener) {
mListListener.add(aListener);
}
private void sendListener(Contact item, int position) {
for(int i = mListListener.size()-1; i >= 0; i--) {
mListListener.get(i).onClickNom(item, position);
}
}
}
package com.example.joignabilite;
导入java.util.ArrayList;
导入java.util.List;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.LinearLayout;
导入android.widget.TextView;
公共类ContactAdapter扩展了BaseAdapter{
//未列出联系人
私人名单;
//Le contexte dans lequel est présent notre适配器
私有上下文;
//联合国版式XML的图形设计
私人停车场;
公共ContactAdapter(上下文,列表aListP){
mContext=上下文;
mListP=aListP;
mInflater=LayoutInflater.from(mContext);
}
public int getCount(){
返回mListP.size();
}
公共对象getItem(int位置){
返回mListP.get(位置);
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
线性布局布局项目;
//(1) :Ré布局的利用率
if(convertView==null){
//初始化notre项目作为布局XML的一部分“Contact_layout.XML”
layoutItem=(LinearLayout)mInflater.充气(R.layout.item_contact,parent,false);
}否则{
layoutItem=(LinearLayout)转换视图;
}
//(2) :Récupération des TextView de notre布局
TextView tv_Nom=(TextView)layoutItem.findviewbyd(R.id.repertoire_项);
//(3) :瓦卢尔重新签署
tv_Nom.setText(mListP.get(position.Nom));
//在mémorise la position de la“Contact”dans le composant textview上
电视名称设置标签(位置);
//关于一个联合国听者
电视名称setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//Lorsque l'on集团在“接触”的位置上
整数位置=(整数)v.getTag();
//在“电视名称”文本视图上的欧盟联合国气候变化委员会(eu un clic)。
sendListener(mListP.get(position),position);
}
});
//关于重述“cré项目”。
返回布局项;
}
/**
*接触点名称上的电气设备接口
*/
公共接口接触器适配器侦听器{
公共void onClickNom(联系人项目,内部位置);
}
//继续收听听众
private ArrayList mListListener=new ArrayList();
/**
*将外部监听器倒在适配器上
*/
public void addListener(ContactAdapterListener Alitener){
mListListener.add(alitener);
}
私有void sendListener(联系人项目,int位置){
对于(int i=mListListener.size()-1;i>=0;i--){
mListListener.get(i).onClickNom(项目、位置);
}
}
}
您的mListP
内部ContactAdapter
为null这就是导致null点异常的原因
原因:在activity类的onCreate
方法中创建ContactAdapter
的新对象之前,您需要初始化listP
。粘贴ContactAdapter
的代码。我把它放在这里,但我认为问题在于我的Repertoire.java的onCreate()上……谢谢,我再也没有这个错误了,但是我总是在我的视图列表中看不到我的联系人。我尝试在我的thred(onCreate)中添加adapter.notifyDataSetChanged(),但它没有刷新listView,我有:android.view.ViewRootImpl$CalledFromErrorThreadException:只有创建视图层次结构的原始线程可以触摸其视图。是的,您不能从非UI线程调用DataNotifyDataSetChanged。您应该使用runOnUiThread
在您的UI线程上调度与视图相关的调用。好的,我认为我的方法很好!我正在尝试在我的线程中使用getServerData(),并在runOnUiThread中使用我的notifyDataSetChanged,但我现在看到了:“适配器的内容已更改,但ListView未收到通知。请确保适配器的内容未从后台线程修改,而仅从UI线程修改。”为了在我的案例中使用它,您可以更精确一些吗?在runOnUiThread中也包括用于移除/插入适配器项目的代码
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="horizontal"
tools:context=".Repertoire" >
<TableLayout
android:id="@+id/repertoire"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include android:id="@+id/header"
layout="@layout/header_mobile"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="@dimen/small_padding"
android:text="@string/destinataire"
android:textSize="@dimen/normal_text_size"
android:textStyle="bold" />
<ListView
android:id="@+id/repertoire_nom"
android:layout_width="fill_parent"
android:layout_height="275dp"
android:paddingTop="@dimen/small_padding" >
</ListView>
<Button android:id="@+id/autre_destinataire"
android:layout_width="wrap_content"
android:layout_height="@dimen/button_height"
android:layout_marginTop="@dimen/small_padding"
android:layout_marginLeft="@dimen/small_padding"
android:layout_marginRight="@dimen/small_padding"
android:background="@drawable/buttontype1"
android:text="@string/autre_destinataire" />
</TableLayout>
</RelativeLayout>
05-12 02:36:50.625: D/memalloc(2205): /dev/pmem: Mapped buffer base:0x5c58e000 size:3768320 offset:0 fd:66
05-12 02:36:50.795: I/System.out(2205): 2null
05-12 02:36:50.795: D/AndroidRuntime(2205): Shutting down VM
05-12 02:36:50.795: W/dalvikvm(2205): threadid=1: thread exiting with uncaught exception (group=0x40ab2210)
05-12 02:36:50.925: E/AndroidRuntime(2205): FATAL EXCEPTION: main
05-12 02:36:50.925: E/AndroidRuntime(2205): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.joignabilite/com.example.joignabilite.Repertoire}: java.lang.NullPointerException
05-12 02:36:50.925: E/AndroidRuntime(2205): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
05-12 02:36:50.925: E/AndroidRuntime(2205): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
05-12 02:36:50.925: E/AndroidRuntime(2205): at android.app.ActivityThread.access$600(ActivityThread.java:127)
05-12 02:36:50.925: E/AndroidRuntime(2205): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
05-12 02:36:50.925: E/AndroidRuntime(2205): at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 02:36:50.925: E/AndroidRuntime(2205): at android.os.Looper.loop(Looper.java:137)
05-12 02:36:50.925: E/AndroidRuntime(2205): at android.app.ActivityThread.main(ActivityThread.java:4441)
05-12 02:36:50.925: E/AndroidRuntime(2205): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 02:36:50.925: E/AndroidRuntime(2205): at java.lang.reflect.Method.invoke(Method.java:511)
05-12 02:36:50.925: E/AndroidRuntime(2205): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
05-12 02:36:50.925: E/AndroidRuntime(2205): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
05-12 02:36:50.925: E/AndroidRuntime(2205): at dalvik.system.NativeStart.main(Native Method)
05-12 02:36:50.925: E/AndroidRuntime(2205): Caused by: java.lang.NullPointerException
05-12 02:36:50.925: E/AndroidRuntime(2205): at com.example.joignabilite.ContactAdapter.getCount(ContactAdapter.java:33)
05-12 02:36:50.925: E/AndroidRuntime(2205): at android.widget.ListView.setAdapter(ListView.java:464)
05-12 02:36:50.925: E/AndroidRuntime(2205): at com.example.joignabilite.Repertoire.onCreate(Repertoire.java:56)
05-12 02:36:50.925: E/AndroidRuntime(2205): at android.app.Activity.performCreate(Activity.java:4465)
05-12 02:36:50.925: E/AndroidRuntime(2205): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-12 02:36:50.925: E/AndroidRuntime(2205): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
05-12 02:36:50.925: E/AndroidRuntime(2205): ... 11 more
05-12 02:36:52.185: I/System.out(2205): [{"nom":"Bob","numero":"243651874"},{"nom":"Alice","numero":"751489525"}]
05-12 02:36:52.225: I/log_tag(2205): Nom: Bob, Numero: 243651874
05-12 02:36:52.235: W/dalvikvm(2205): threadid=11: thread exiting with uncaught exception (group=0x40ab2210)
05-12 02:36:52.245: I/Process(2205): Sending signal. PID: 2205 SIG: 9
05-12 02:36:52.415: D/dalvikvm(2308): GC_FOR_ALLOC freed 35K, 4% free 13167K/13663K, paused 20ms
05-12 02:36:52.415: I/dalvikvm-heap(2308): Grow heap (frag case) to 14.705MB for 1858832-byte allocation
05-12 02:36:52.445: D/dalvikvm(2308): GC_FOR_ALLOC freed 1K, 4% free 14981K/15523K, paused 14ms
05-12 02:36:52.485: D/dalvikvm(2308): GC_CONCURRENT freed 454K, 6% free 14627K/15523K, paused 1ms+2ms
package com.example.joignabilite;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ContactAdapter extends BaseAdapter {
// Une liste de Contacts
private List<Contact> mListP;
//Le contexte dans lequel est présent notre adapter
private Context mContext;
//Un mécanisme pour gérer l'affichage graphique depuis un layout XML
private LayoutInflater mInflater;
public ContactAdapter(Context context, List<Contact> aListP) {
mContext = context;
mListP = aListP;
mInflater = LayoutInflater.from(mContext);
}
public int getCount() {
return mListP.size();
}
public Object getItem(int position) {
return mListP.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout layoutItem;
//(1) : Réutilisation des layouts
if (convertView == null) {
//Initialisation de notre item à partir du layout XML "Contact_layout.xml"
layoutItem = (LinearLayout) mInflater.inflate(R.layout.item_contact, parent, false);
} else {
layoutItem = (LinearLayout) convertView;
}
//(2) : Récupération des TextView de notre layout
TextView tv_Nom = (TextView)layoutItem.findViewById(R.id.repertoire_item);
//(3) : Renseignement des valeurs
tv_Nom.setText(mListP.get(position).nom);
//On mémorise la position de la "Contact" dans le composant textview
tv_Nom.setTag(position);
//On ajoute un listener
tv_Nom.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Lorsque l'on clique sur le nom, on récupère la position de la "Contact"
Integer position = (Integer)v.getTag();
//On prévient les listeners qu'il y a eu un clic sur le TextView "TV_Nom".
sendListener(mListP.get(position), position);
}
});
//On retourne l'item créé.
return layoutItem;
}
/**
* Interface pour écouter les évènements sur le nom d'une Contact
*/
public interface ContactAdapterListener {
public void onClickNom(Contact item, int position);
}
//Contient la liste des listeners
private ArrayList<ContactAdapterListener> mListListener = new ArrayList<ContactAdapterListener>();
/**
* Pour ajouter un listener sur notre adapter
*/
public void addListener(ContactAdapterListener aListener) {
mListListener.add(aListener);
}
private void sendListener(Contact item, int position) {
for(int i = mListListener.size()-1; i >= 0; i--) {
mListListener.get(i).onClickNom(item, position);
}
}
}