Android 从联机数据库填充listview
我是编程新手,我已经尝试了很长一段时间,但我还没有解决它,所以,我现在想把数据从我的在线数据库传输到一个列表视图并显示它。我已经检查了我的php文件,它运行良好。它返回了我想要的名称,而且代码似乎运行良好,直到我到达Baseadapter部分 超饭后活动 活动VerPostSuario XML ListView 活动\u主要\u项目XML 最初,本教程还演示了如何显示图像,这就是为什么有导入SmartImageView和Rect的原因,因此我删除了此部分,并尝试仅使用文本来执行此操作。也许我在某个地方搞砸了,但我找不到地方,所以如果你能帮我解决这个问题,我将非常感激 编辑: 因此,我想感谢所有试图帮助我的人,似乎阵列和Baseadapter都有一些问题。我将在下面列出任何有类似问题的人 -数组: 有一个1而不是一个i,必须更换 我试图从数据库中获取一个名为nombreAnimal的字段,但实际上在我的php文件中,我已将nombreAnimal分配给Nombre nombreAnimal.addjsonArray.getjsonobject.getStringNombre 我还发现,检查你的数组是否有你想要的值的最好方法是检查Android的Logcat -基本适配器: 我忘了从getCount数组中的数组中获取大小 我忘记了在getItem中,我需要从我的数组nombreAnimal array.geti返回位置Android 从联机数据库填充listview,android,listview,Android,Listview,我是编程新手,我已经尝试了很长一段时间,但我还没有解决它,所以,我现在想把数据从我的在线数据库传输到一个列表视图并显示它。我已经检查了我的php文件,它运行良好。它返回了我想要的名称,而且代码似乎运行良好,直到我到达Baseadapter部分 超饭后活动 活动VerPostSuario XML ListView 活动\u主要\u项目XML 最初,本教程还演示了如何显示图像,这就是为什么有导入SmartImageView和Rect的原因,因此我删除了此部分,并尝试仅使用文本来执行此操作。也许我在某
我忘了在getItemdId中返回I 我假设除了适配器的实现之外,其他一切都是正确的 您没有正确地重写这些方法,我让您使用代码来修复它 图像适配器中的代码: 我希望它能对您有所帮助。查看您的BaseAdapter,如下所示;你可以找到更多信息 请尝试以下代码:
public class VerPostsUsuario extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ver_posts_usuario);
listView=(ListView)findViewById(R.id.listView);
descargarImagen();
}
private ListView listView;
ArrayList nombreAnimal = new ArrayList();
private void descargarImagen(){
nombreAnimal.clear();
final ProgressDialog progressDialog = new ProgressDialog(VerPostsUsuario.this);
progressDialog.setMessage("Cargando Datos");
progressDialog.show();
AsyncHttpClient client = new AsyncHttpClient();
client.get("mydomain+php", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
if(statusCode==200){
progressDialog.dismiss();
try {
JSONArray jsonArray = new JSONArray(new String(responseBody));
for (int i=0; 1<jsonArray.length();i++){
nombreAnimal.add(jsonArray.getJSONObject(i).getString("nombreAnimal"));
}
listView.setAdapter(new ImagenAdapter(getApplicationContext(), nombreAnimal));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
}
});
}
private class ImagenAdapter extends BaseAdapter {
Context ctx;
LayoutInflater layoutInflater;
TextView tvnombreAnimal;
ArrayList list;
public ImagenAdapter(Context applicationContext, ArrayList list) {
this.ctx=applicationContext;
this.list = list;
layoutInflater=(LayoutInflater)ctx.getSystemService(LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View convertView, ViewGroup parent) {
ViewGroup viewGroup =(ViewGroup)layoutInflater.inflate(R.layout.activity_main_items, null);
tvnombreAnimal = (TextView) viewGroup.findViewById(R.id.tvnombreAnimal1);
tvnombreAnimal.setText(list.get(i).toString());
return viewGroup;
}
我刚刚在您的内部类中添加了额外的arraylist。我认为数组和处理服务器响应的方式有问题。我就是这样做的 似乎您正在尝试获取值nombreAnimal而不是Nombre。试试这个:
try {
JSONArray jsonArray = new JSONArray(new String(responseBody));
for (int i = 0; i < jsonArray.length(); i++) {
nombreAnimal.add(jsonArray.getJSONObject(i).getString("Nombre"));
}
listView.setAdapter(new ImagenAdapter(getApplicationContext()));
} catch (JSONException e) {
e.printStackTrace();
}
我希望它能帮助你 它似乎仍然不起作用,你认为基本适配器部分应该正常工作吗?活动主项上的ContrainLayout与此有关吗?在本教程中,使用了线性布局,您必须将其添加到ImagenAdapter中。但是可能您的代码或xml布局中还有其他错误。好的,我将再次检查代码中的错误,并尝试找出数组中是否有错误。感谢您的帮助这似乎也不起作用,idk如果阵列可能有问题,我怎样才能将名称放入普通的文本视图中检查数据是否存在?能否打印这一行以确保获得json数据jsonArray.getJSONObjecti.getStringnombreAnimal@archesth177抱歉,您的无知是指将其显示在文本视图或其他可以看到数据的地方?是的,只需在调试控制台中打印即可,使用Log.v或System.outSo我使用一个字符串来存储该行,然后使用带有tag:tag和id:id的Log+我创建的字符串,然后启动调试器并搜索tag,它显示这两次ViewPostMeInputStage操作失败,但没有发生任何事情,代码上似乎没有错误,但无论出于何种原因,它都无法工作。您知道如何将数组nombreAnimal数据放入一个普通的文本视图中,以检查其上是否有任何数据吗?请注意,在for int i=0中有一个小错误;1谢谢你,我没有注意到,它不起作用,但它朝着解决这个问题的正确方向迈出了一步是的!现在,在日志中显示了这些值,但它显示了此错误。org.json.JSONArray类型的值[{Nombre:Carlos},{Nombre:Juan}]无法转换为JSONObject。Idk在我的例子中结果字段是什么查看您的其他评论,我注意到您正在尝试获取值nombrenimal,我认为您应该将Nombre设置为。我会更新我上面的答案,这样你就可以看到代码应该是怎样的。谢谢你!!!它最终成功了,你是对的,这是我放在php文件中的字段,而不是从我的数据库中。教程解释说,在该字段中,我应该放我的db字段的名称。这就是为什么它不起作用!你帮了我很多,伙计,我非常感谢你!
<TextView
android:id="@+id/tvnombreAnimal1"
android:layout_width="177dp"
android:layout_height="39dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="8dp"
android:layout_marginLeft="148dp"
android:layout_marginStart="148dp"
android:layout_marginTop="8dp"
android:textColor="#000"
android:textSize="24dp"
/>
//You need to return the size of the array, if you always are returning 0 the adapter dont
//print any because they "think" the array is empty.
@Override
public int getCount() {
return nombreAnimal.size();
}
//This method helps you to return the Item at specific index
@Override
public Object getItem(int i) {
return nombreAnimal.get(i);
}
@Override
public int getCount() {
return nombreAnimal.size();
}
@Override
public Object getItem(int position) {
return nombreAnimal.get(position);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// inflate the layout for each list row
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.activity_main_items, null);
}
TextView tvnombreAnimal = (TextView)
convertView.findViewById(R.id.tvnombreAnimal1);
tvnombreAnimal.setText(nombreAnimal.get(i).toString());
// returns the view for the current row
return convertView;
}
public class VerPostsUsuario extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ver_posts_usuario);
listView=(ListView)findViewById(R.id.listView);
descargarImagen();
}
private ListView listView;
ArrayList nombreAnimal = new ArrayList();
private void descargarImagen(){
nombreAnimal.clear();
final ProgressDialog progressDialog = new ProgressDialog(VerPostsUsuario.this);
progressDialog.setMessage("Cargando Datos");
progressDialog.show();
AsyncHttpClient client = new AsyncHttpClient();
client.get("mydomain+php", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
if(statusCode==200){
progressDialog.dismiss();
try {
JSONArray jsonArray = new JSONArray(new String(responseBody));
for (int i=0; 1<jsonArray.length();i++){
nombreAnimal.add(jsonArray.getJSONObject(i).getString("nombreAnimal"));
}
listView.setAdapter(new ImagenAdapter(getApplicationContext(), nombreAnimal));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
}
});
}
private class ImagenAdapter extends BaseAdapter {
Context ctx;
LayoutInflater layoutInflater;
TextView tvnombreAnimal;
ArrayList list;
public ImagenAdapter(Context applicationContext, ArrayList list) {
this.ctx=applicationContext;
this.list = list;
layoutInflater=(LayoutInflater)ctx.getSystemService(LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View convertView, ViewGroup parent) {
ViewGroup viewGroup =(ViewGroup)layoutInflater.inflate(R.layout.activity_main_items, null);
tvnombreAnimal = (TextView) viewGroup.findViewById(R.id.tvnombreAnimal1);
tvnombreAnimal.setText(list.get(i).toString());
return viewGroup;
}
try {
JSONArray jsonArray = new JSONArray(new String(responseBody));
for (int i = 0; i < jsonArray.length(); i++) {
nombreAnimal.add(jsonArray.getJSONObject(i).getString("Nombre"));
}
listView.setAdapter(new ImagenAdapter(getApplicationContext()));
} catch (JSONException e) {
e.printStackTrace();
}