2

私は自分のデータベースから自分のウェブサイトから自分のアプリに情報を取得することに成功したいくつかのチュートリアルに従ってきました、そしてそれをリストビューに表示することができます、しかし私がしたいのは完全なリストを表示することではありません、私はしたい特定の条件を満たすデータのみを表示できるボタンがいくつかあります。たとえば、下の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=periodicoのものだけ、そこからmedio =radioTahnksを選ぶために反対のことをすることができるかもしれません

4

4 に答える 4

4

このコードをテストすることはできませんが、ここに機能する可能性のあるものがあります

1)フィルタリングされたアイテムのみを保持する別のリストを作成します

public ... ArrayList<webResult> FilteredArrayOfWebItems;

2)「タイプ」(periodicoまたはradio)文字列タイプを設定できるメソッドをアダプタに作成します。

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)get Viewで、フィルターされたリストから情報を読み取ります

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

5)フィルターを変更するときはいつでも、メインメソッドからSetTypeを呼び出します

6)メインメソッドからadapter.notifyDataSetChanged()、フィルターを変更した後にアダプターを呼び出します

于 2012-04-30T17:35:53.903 に答える
0

レイアウトフォルダ fake.xmlに1つの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);
}
于 2012-04-30T17:40:00.423 に答える
0

カスタマイズされたリストアクティビティで、不要な要素を削除し、リストを一時リストに保存して、その一時リストをアダプタに渡します。

于 2012-05-25T20:30:39.083 に答える
0

これを行うと十分です:

if(position >2)
  {
        return convertView;
   }

3つを超える結果は表示されません(0,1,2)

于 2018-03-02T18:36:38.360 に答える