android - 如何使用来自不同 xml 布局文件的 ListView

标签 android listview xml-layout

我正在使用 android ListView,我遇到了一个问题。我在 XML 文件 installation.xml 中创建了一个 ListView ,我想使用该 ListView 进入我的 Searchdata.java。所以基本上我想要的是当我点击搜索数据按钮时,数据是从网络服务中获取的,解析后,它会保存到 ListView 中。当我点击安装 View 按钮,然后新窗口将出现在我可以看到该列表数据的地方。

SearchData.java

public class SearchData extends Activity {
EditText Keyword;
JSONParser jsonparser = new JSONParser();
ListView Datalist;
HorizontalScrollView VideoDatalist;
ArrayList<HashMap<String, String>> DataList;
ArrayList<HashMap<String, String>> VideoDataList;
JSONArray contacts = null;
private ProgressDialog pDialog;
ImageButton searchdata,InstallationView;
String Keyvalue = new String();
private static final String TAG_InnerText = "InnerText";
private static final String TAG_Title = "Title";
private static final String TAG_URL = "URL";
private static final String TAG_VIDEO_URL = "URL";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_data);
        InstallationView=(ImageButton)findViewById(R.id.InstallationView);
        Keyword = (EditText) findViewById(R.id.KeyData);
        Datalist=(ListView)findViewById(R.layout.activity_installation);
        VideoDatalist=(HorizontalScrollView)findViewById(R.id.Horizontallist);
        searchdata=(ImageButton)findViewById(R.id.searchicon);
            String Keyvalue = new String();
        DataList = new ArrayList<HashMap<String, String>>();
        VideoDataList = new ArrayList<HashMap<String, String>>();



        searchdata.setOnClickListener(new View.OnClickListener()   {             
            public void onClick(View v) {

                    new ReadData().execute();                           
            }  
             });



        InstallationView.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v)
            {
                startActivity(new Intent(SearchData.this, Installation.class)); 
            }

            });
    }


    public class ReadData extends AsyncTask<Void, Void, Void>
    {
         @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(SearchData.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();

        }

        protected Void doInBackground(Void... arg0) {

           try{
               Keyvalue=Keyword.getText().toString();

                String Jsonstr = jsonparser.makeHttpRequest("http://10.47.93.26:8080/Search/api/Search/"+Keyvalue);


                try {
                if (Jsonstr != null) {

                    JSONArray    jsonObj = new JSONArray (Jsonstr);

                for (int i = 0; i < jsonObj.length(); i++) {
                            JSONObject c = jsonObj.getJSONObject(i);


                            String name = c.optString(TAG_Title);
                            String url = c.optString(TAG_URL);

                            HashMap<String, String> info = new HashMap<String, String>();


                            if( !name.isEmpty() )
                            {
                            info.put(TAG_Title, name);
                            }
                            else
                            {
                                info.put(TAG_Title,"User Manual");
                            }


                            if(url.contains("youtube"))
                            {

                                info.put(TAG_URL, url);
                                VideoDataList.add(info);

                            }
                            else
                            {

                            info.put(TAG_URL, url);
                            DataList.add(info);
                            }

                        }

                    }   

                    else {
                        Log.e("ServiceHandler", "Couldn't get any data from the url");
                    }
                }
                catch (JSONException e) {
                    e.printStackTrace();
                }
           }
           catch(Exception ex)
           {
               ex.printStackTrace();
           }

   return null;
    }


        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            if (pDialog.isShowing())
                pDialog.dismiss();

            SimpleAdapter adapter = new SimpleAdapter(
                    SearchData.this, DataList,
                    R.layout.list_item, new String[]
                            {   
                            TAG_Title
                             }, new int[] { 
                            R.id.InnerText });
            Datalist.setAdapter(adapter);

        }
    }
}

网络服务运行和解析代码运行正常。我在 post 方法中遇到错误,所以你能帮我解决这个问题吗?

错误

enter image description here

最佳答案

在 onClick() 方法中调用您的安装 Activity :

并通过 Intent 传递您的 ArrayList 数据,

        InstallationView.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v)
        {
            Intent intent= new Intent(SearchData.this, Installation.class);
            intent.putParcelableArrayListExtra("HASH_MAP",DataList);
            startActivity(intent); 
        }

        });

在您的 Installation Activity 类中,在 onCreate() 中设置 View 并从 xml 文件初始化 ListView :

  setContentView(R.layout.activity_installation);
  ListView listView = (ListView)findViwById(R.id.listview);

并尝试从 Intent 中获取数据:

 ArrayList<HashMap<String,String>> hashmap_dataList = getIntent.getParcelableArrayListExtra("HASH_MAP");

然后用 listview 和 hashmap 做任何你想做的事。

关于android - 如何使用来自不同 xml 布局文件的 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29470136/

相关文章:

android - 是否可以将可滚动的 TextView 添加到 ListView?

java - 从线程启动 Android Activity

Android listview项目背景改变

安卓测试 : updating listview in a broadcast receiver onreceive

java - Android REST API 连接

Android SimpleCursorAdapter - 限制 ListView 结果

qt - Qt/QML 中的下拉刷新 ListView

android - TypedArray 不工作

android - 在 Android GridLayout 中水平居中 View

java - android中表单程序生成的布局到XML布局