ListView OnItemClickListener选择json对象时崩溃

时间:2018-01-01 18:11:47

标签: java android json listview android-intent

当我尝试从searchActivity.javasearchDetail.java检索要使用的JSON对象时,应用程序崩溃了。函数getInfo()似乎导致了这个问题。最初,getInfo()中的list_data的大小为10,但是在从searchActivity.java调用后,大小变为0.因此,我可以知道如何解决此问题?

DataParser.java

public class DataParser extends AsyncTask<Void,Void,Integer> {
Context c;
ArrayList<Herb> herb=new ArrayList<>();
ArrayList<HashMap<String, String>> list_data = new ArrayList<HashMap<String, String>>();
HashMap<String,String> data = new HashMap<>();

public DataParser(ListView lv){
    this.lv = lv;
}

public DataParser(Context c, String jsonData, ListView lv) {
    this.c = c;
    this.jsonData = jsonData;
    this.lv = lv;
}
@Override
protected Integer doInBackground(Void... params) {
    return this.parseData();
}
@Override
protected void onPostExecute(Integer result) {
    super.onPostExecute(result);
    pd.dismiss();
    if(result==0)
    {
        Toast.makeText(c,"Unable To Parse",Toast.LENGTH_SHORT).show();
    }else {
        //BIND DATA TO LISTVIEW
        CustomAdapter adapter=new CustomAdapter(c,herb);
        lv.setAdapter(adapter);
    }
}
private int parseData()
{
    try
    {
        JSONArray ja=new JSONArray(jsonData);
        JSONObject jo=null;
        herb.clear();
        Herb Herb;
        for(int i=0;i<ja.length();i++)
        {
            jo=ja.getJSONObject(i);
            String id=jo.getString("h_id");
            String name=jo.getString("h_name");
            String imageUrl=jo.getString("h_image");
            Herb=new Herb();
            Herb.setId(id);
            Herb.setName(name);
            Herb.setImageUrl(imageUrl);
            herb.add(Herb);

            data.put("id",id);
            data.put("name",name);
            data.put("imageUrl",imageUrl);
           list_data.add(data);
        }
        DataStorage.map = getInfo();
        return 1;
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return 0;
}
public ArrayList<HashMap<String, String>> getInfo(){
    return this.list_data = list_data;
}

}

searchActivity.java

public class searchActivity extends AppCompatActivity{

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

    ListView lv= (ListView) findViewById(R.id.HerbListView);
    Downloader dl = new Downloader(searchActivity.this,urlAddress,lv);
    dl.execute();
    final DataParser dp = new DataParser(lv);
    lv.setOnItemClickListener (new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        try{
            ArrayList<HashMap<String, String>> list_data = DataStorage.map;
            String hid = list_data.get(position).get("id");
            String name = list_data.get(position).get("name");
            String imageUrl = list_data.get(position).get("imageUrl");

            Intent intent = new Intent(searchActivity.this, searchDetail.class);
            intent.putExtra("id", hid);
            intent.putExtra("name", name);
            intent.putExtra("imageUrl", imageUrl);
            startActivity(intent);
        }
        catch (NullPointerException e){
            e.printStackTrace();
        }
    }
});
}

}

searchDetail.java

public class searchDetail extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_detail);
    String name = getIntent().getStringExtra("name");
    String imageUrl = getIntent().getStringExtra("imageUrl");

    TextView txt1= (TextView) findViewById(R.id.txtHName);
    ImageView img= (ImageView) findViewById(R.id.imageHerb);

    txt1.setText(name);
    Picasso.with(this).load(imageUrl).fit().into(img);
}

}

DataStorage.java

public class DataStorage {
public static ArrayList<HashMap<String,String>> map = new ArrayList<>();

}

activity_search_detail.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageHerb"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerHorizontal="true"
        android:scaleType="fitXY" />

    <TextView
        android:id="@+id/lblHName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/imageHerb"
        android:paddingTop="10dp"
        android:text="Herb Name: " />

    <TextView
        android:id="@+id/txtHName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/imageHerb"
        android:layout_toEndOf="@id/lblHName"
        android:paddingTop="10dp"
        android:text="TextView" />


</RelativeLayout>

当前生成的logcat

java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout

P / S:我无法在activity_search_detail.xml

中找到任何循环依赖项

2 个答案:

答案 0 :(得分:0)

首先执行DataParser类.. new DataParser( lv).execute()或在你的情况dp.execute()中你缺少这个语句..如果你没有执行AsyncTask类,它就不会在doInBackground()中执行任何任务。希望它能解决您的问题.. dp.getinfo()将始终返回一个空白列表...创建另一个名为class holdHashmapList { public static ArrayList<Hashmap<String,String>> map= new ArrayList<>();}的类,并在dataParser类holdHashmapList.map =getInfo();}和onItemClick侦听器中... {{1你正在构建的hashmap将始终包含Arralist<map<>> = holdHashmapList.map;函数中的最后一个对象...所以在parseData之前添加data.put("id");

答案 1 :(得分:0)

您必须执行DataParser(dp)才能在SearchActivity中填充list_data。只有初始化DataParser不会填充list_data。

public class searchActivity extends AppCompatActivity{

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

    ListView lv= (ListView) findViewById(R.id.HerbListView);
    Downloader dl = new Downloader(searchActivity.this,urlAddress,lv);
    dl.execute();
    final DataParser dp = new DataParser(lv);
    dp.execute();     // update 
    lv.setOnItemClickListener (new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long 
                         id) {
        try{
            ArrayList<HashMap<String, String>> list_data = dp.getInfo();
            String hid = list_data.get(position).get("id");
            String name = list_data.get(position).get("name");
            String imageUrl = list_data.get(position).get("imageUrl");

            Intent intent = new Intent(searchActivity.this, searchDetail.class);
            intent.putExtra("id", hid);
            intent.putExtra("name", name);
            intent.putExtra("imageUrl", imageUrl);
            startActivity(intent);
        }
        catch (NullPointerException e){
            e.printStackTrace();
        }
     }
    });
  }