3

行の色を変更したかったので、独自のSimpleAdapterオブジェクトを作成する前は、新しいSimpleAdapter(...)を使用していました。独自のカスタムSimpleAdapterを使用しているので、行の色は変わりますが、テキストは更新されません。私はadapter.notifyDataSetChanged()を呼び出しましたが、それでもサンプルテキスト「TextView」のみが表示されています。私が言ったように、私が自分のアダプターを作成しなかったとき、すべてがうまく機能していました。私が初期化している順序と関係があるのではないかと思います。

public class AddScreen extends Activity implements OnClickListener,
    OnItemClickListener, OnItemLongClickListener {
SimpleAdapter adapter;
List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();
ListView listthings;
int[] to;
    String[] from;

public void onCreate(Bundle savedInstanceState) {
...
listthings = (ListView) findViewById(R.id.listthings);
    from = new String[] { "row_1", "row_2" };
    to = new int[] { R.id.row1, R.id.row2 };

    adapter = new Adapter(this, painItems, R.layout.mylistlayout,
            from, to);

    listthings.setAdapter(adapter);
...
}

public class Adapter extends SimpleAdapter{
    HashMap<String, String> map = new HashMap<String, String>();
    public Adapter(Context context, List<? extends Map<String, String>> data,
            int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);

    }
@Override
    public View getView(int position, View convertView, ViewGroup parent){
        View row = convertView;
        if (row == null) {
            LayoutInflater mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = mInflater.inflate(R.layout.mylistlayout, parent, false);
            }
        row.setBackgroundColor(0xFF0000FF);
       TextView rw1 = (TextView)findViewById(R.id.row1);
      // TextView rw2 = (TextView)findViewById(R.id.row2);
       rw1.setText(map.get(position));
       return row;
    }

}
// to add the item, put it in the map, and add the map into the list
private void addItem() {
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("row_1", row1);
    map.put("row_2", row2);
    map.put("row_3", painLevelString);
    map.put("row_4", painLocation);
    map.put("row_5", timeOfPainString);
    map.put("row_6",textTreatmentString);
    painItems.add(map);

        adapter.notifyDataSetChanged();




}

編集:追加されたコード

これは、addItemコードの前に配置されたintent(onActivityResult())からデータを取得する方法です。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == 1) {
        row1 = data.getStringExtra("com.painLogger.row1");
        row2 = data.getStringExtra("com.painLogger.row2");

        painLevelString = data.getStringExtra("com.painLogger.painLevel");
        painLocation = data.getStringExtra("painLocation");
        timeOfPainString = data.getStringExtra("com.painLogger.painTime");
        textTreatmentString = data
                .getStringExtra("com.painLogger.treatment");
        addItem();
    }
}

*また、これが関連する場合に備えて、配置の順序は次のとおりです。onCreate()->カスタムアダプタクラス-> onActivityResult()-> addItem()* **

これがどのように見えるかのスクリーンショットです。各アイテムの2つのTextViewフィールドには、情報を入力する必要があります(これを行うまでは、情報でした)。

4

3 に答える 3

5

以前にそれを使用して機能した場合new SimpleAdapter(...)は、getView(...)実装で最初の行を次のように変更します。

View row = super.getView(position, convertView, parent);

そして、それがあなたが期待しているものであるかどうかを確認してください。ものも取り出してくださいLayoutInflater

于 2011-08-02T22:04:30.900 に答える
1

getView()で、行の背景を設定する場所については、のテキストも設定する必要がありますTextView

を呼び出す notifyDataSetChanged()と、テキストが自動的に正しく設定されるわけではなく、表示されているListView行が新しいデータで再描画されるだけです...実際getView()には、更新が必要な各行が呼び出されます。

また、ファイルから背景色を設定することをお勧めしmylistlayout.xmlます。getView()関数が数findViewByID秒かかり始めた場合は、次のサンプルのような「ビューホルダー」アプローチの使用も検討する必要があります:http://developer.android。 com / resources / samples / ApiDemos / src / com / example / android / apis / view / List14.html

于 2011-08-02T18:22:59.857 に答える
1

でテキストを設定する必要がありますgetView()。このような:

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

    TextView text;

    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.mylistlayout, parent, false);
        text = (TextView) convertView.findViewById(R.id.more_list_text);
    }
    convertView.setBackgroundColor(0xFF0000FF);
    text.setText(map.get(position));
    return convertView;
}

また、これは非常に重要です。つまり、マップをSimpleAdapter
ieのメンバー変数として格納し、次の行をオブジェクト定義の先頭に配置します。

HashMap<String, String> map = new HashMap<String, String>();
于 2011-08-02T19:47:08.043 に答える