Android如何从同一个数组中仅显示列表视图中的某些项目?

问题描述 投票:2回答:4

我一直在关注几个教程,这些教程让我成功地将我的数据库中的信息从我的网站获取到我的应用程序,然后我可以在列表视图中显示它,但我想要做的不是显示完整列表,我想要有几个按钮,让我只显示符​​合特定条件的数据,如底部1只显示列表项medio = Periodicos和按钮2显示项目medio = Radios这里是我正在使用的代码

在我的主要活动中

FancyAdapter aa = null;
static ArrayList<String> resultRow;

//after onCreate
//this button I'm usin now but I'm getting the hole list!
Button periodicos = (Button) findViewById(R.id.btnPeriodicos);
            periodicos.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                weballamar = "http://www.mysite.com/mydbjsonstring.php";
                webcall();
            }});

//webcall gets the info from the internet and displays the list
public void webcall(){
    ListView myListView = (ListView) findViewById(R.id.mylistView);
    aa = new FancyAdapter();
    myListView.setOnItemClickListener(onPeriodicosListClick);
    myListView.setAdapter(aa);
    PeriodicosListLayout.setVisibility(View.VISIBLE);
    webLayout.setVisibility(View.GONE);

    try {
        String result = "";
                    try {
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost(weballamar);
                        // httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                        HttpResponse response = httpclient.execute(httppost);
                        HttpEntity entity = response.getEntity();
                        InputStream webs = entity.getContent();
                        // convert response to string
                        try {
                            BufferedReader reader = new BufferedReader(
                                    new InputStreamReader(webs, "iso-8859-1"), 8);
                            StringBuilder sb = new StringBuilder();
                            String line = null;
                            while ((line = reader.readLine()) != null) {
                                sb.append(line + "\n");
                            }
                            webs.close();

                            result = sb.toString();
                        } catch (Exception e) {
                            Log.e("log_tag", "Error converting result " + e.toString());
                        }
                    } catch (Exception e) {
                        Log.e("log_tag", "Error in http connection " + e.toString());
                    }

                    // parse json data
                    try {
                        JSONArray jArray = new JSONArray(result);
                        for (int i = 0; i < jArray.length(); i++) {
                            JSONObject json_data = jArray.getJSONObject(i);
                            webResult resultRow = new webResult();
                            resultRow._id = json_data.getString("id");
                            resultRow.Name = json_data.getString("Id_Nombre");
                            resultRow.Medio = json_data.getString("Medio");
                            resultRow.Pais = json_data.getString("Pais");
                            resultRow.Estado = json_data.getString("Estado");
                            resultRow.Ciudad = json_data.getString("Ciudad");
                            resultRow.website = json_data.getString("website");
                            resultRow.Url = json_data.getString("Url");
                            resultRow.Url2 = json_data.getString("Url2");
                            resultRow.InfAnex = json_data.getString("InfAnex");
                            arrayOfWebData.add(resultRow);
                        }
                    } catch (JSONException e) {
                        Log.e("log_tag", "Error parsing data " + e.toString());
                    }
    } catch (Exception e) {
        // this is the line of code that sends a real error message to the
        // log
        Log.e("ERROR", "ERROR IN CODE: " + e.toString());
        // this is the line that prints out the location in
        // the code where the error occurred.
        e.printStackTrace();
    }
}

class FancyAdapter extends ArrayAdapter<webResult> {
    FancyAdapter() {
        super(mainActivity.this,
                android.R.layout.simple_list_item_1, arrayOfWebData);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            LayoutInflater inflater = getLayoutInflater();
            convertView = inflater.inflate(R.layout.listitem, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.populateFrom(arrayOfWebData.get(position));

        return (convertView);
    }
}

class ViewHolder {
    public TextView name = null;
    public TextView estado = null;
    public TextView ciudad = null;
    ViewHolder(View row) {
        name = (TextView) row.findViewById(R.id.periodicoName);
        estado = (TextView) row.findViewById(R.id.periodicoEstado);
        ciudad = (TextView) row.findViewById(R.id.periodicoCiudad);
    }

    void populateFrom(webResult r) {
        name.setText(r.Name);
        estado.setText(r.Estado);
        ciudad.setText(r.Ciudad);
    }
}

你可以看到我在列表视图中显示名称属性和城市列的完整列表,但该列表包含medio列中的项目是定期或无线电,所以我如何使我的适配器区分并选择显示只有那些女巫medio = periodico,从那里我可以做相反的选择medio =无线电Tahnks

android listview android-arrayadapter listadapter
4个回答
4
投票

无法测试此代码,但这里可能有用

1)创建另一个仅包含过滤项的列表

public ... ArrayList<webResult> FilteredArrayOfWebItems;

2)在适配器中创建一个方法,允许您设置“类型”(周期性或无线电)字符串类型;

public void SetType(String type)
{ 
     /* Maybe something like this */
     //Clear the FilteredArrayOfWebItems first. Not on eclipse, at the moment, but I suspect FilteredArrayOfWebItems.clear() might be call you need

    for(JSONObject currentItem: arrayOfWebItems)
    {
        //Check if the Medio property of the current item matches the typ
        if(currentItem.Medio.equals(type))
        {
            //Add it to the Filtered list
            FilteredArrayOfWebItems.add(currentItem);

        }
     }

}

3)您可能需要覆盖getCount()方法以获取已过滤项目的计数,而不是完整列表

4)在获取视图中,从过滤列表中读取信息

holder.populateFrom(FilteredArrayOfWebItems.get(position)); 

5)每当要更改过滤器时,从main方法调用SetType

6)从主方法开始,在更改过滤器后调用适配器上的adapter.notifyDataSetChanged()


0
投票

在自定义列表活动中,删除不需要的元素并将列表存储到某个临时列表中,并将该临时列表传递给适配器。


0
投票

在布局文件夹fake.xml中创建一个xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"> 

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;


    if (convertView == null) {
        LayoutInflater inflater = getLayoutInflater();
        convertView = inflater.inflate(R.layout.listitem, null);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);



    /*****  add this code in your getView method() ********/
      if(position ==3)
      {
            LayoutInflater inflater = getLayoutInflater();
            convertView = inflater.inflate(R.layout.fake, null);
            convertView.setTag(holder);
            returns convertview;
       }
     /*****  add this code in your getView method() ********/



    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.populateFrom(arrayOfWebData.get(position));

    return (convertView);
}

0
投票

如果你这样做就足够了:

if(position >2)
  {
        return convertView;
   }

它不会显示超过3个结果(0,1,2)

© www.soinside.com 2019 - 2024. All rights reserved.