ボタン(または可能であればメソッド)を使用せずに、単一の行を無効にしてリストビューの背景色を変更するにはどうすればよいですか?

クラン

この投稿のタイトルはそれをすべて言います。

このコードは問題なく機能します。

package abc.AvailableCars;

import android.graphics.Color;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class carListActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.car_list_layout);

final ListView carListview = (ListView) findViewById(R.id.list_view);

final Button dButton = (Button) findViewById(R.id.disable_button);

String[] cars = {"Maxima GXE", "Passat", "Focus SE", "Mazda6", "Avalon", :Sentra GXE"};

final List<String> list_of_cars = new ArrayList<String>(Arrays.asList(cars));

final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
        (this, android.R.layout.simple_list_item_1, list_of_cars);

carListview.setAdapter(arrayAdapter);

dButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
int chosenRow = 3;

carListview.getChildAt(3).setEnabled(false);
carListview.getChildAt(3).setBackgroundColor(Color.parseColor("#3f51b5"));

}

});

}

}

これは私のlistview.xmlファイルにあります:

<Button
    android:id="@+id/disable_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Disable A Row"
    />

しかし、以下のようにボタンに属するすべてのものをコメントアウトし、Car Listクラスが呼び出されると、アプリはLogcatのエラーでクラッシュします:java.lang.NullPointerException:仮想メソッド 'voidandroidを呼び出そうとしました。 nullオブジェクト参照のview.View.setEnabled(boolean) ':

final ListView carListview = (ListView) findViewById(R.id.list_view);
//final Button dButton = (Button) findViewById(R.id.disable_button);

String[] cars = {"Maxima GXE", "Passat", "Focus SE", "Mazda6", "Avalon"};

final List<String> list_of_cars = new ArrayList<String>(Arrays.asList(cars));

final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
        (this, android.R.layout.simple_list_item_1, list_of_cars);

carListview.setAdapter(arrayAdapter);

//dButton.setOnClickListener(new View.OnClickListener() {

//@Override
//public void onClick(View v) {

int chosenRow = 3;

carListview.getChildAt(chosenRow).setEnabled(false);
carListview.getChildAt(chosenRow).setBackgroundColor(Color.parseColor("#3f51b5"));

}

//});

}

//}

私はもうAndroidの初心者ではありませんが、これは私にはわかりません。

リストビューが表示されたらすぐに、選択した行を無効にして色を設定したいと思います。

ボタンなしでプログラムでこれを行うにはどうすればよいですか?

私は考えられるすべてのバリエーション、getView()、さらには偽のクリックを試しました。

違いが生じる場合に備えて、このコードはMainActivity.javaファイルとは別のクラスとファイルにあり、そのファイルで呼び出されます。

簡単な答えが必要です。何を変更する必要がありますか?

冗長にしてください。

ありがとうございました。

クラン

私の理解では、listViewはビューであるため、いくつかの変更を加えるにはオーバーライドする必要があります。

必要な行を無効にしないことを選択しましたが、コードでそれらを確認します。

動作する完全なコードは以下のとおりです。
いくつかのクレジットは、Androidでの彼/彼女の答えのためにRaghunandanに行きます-ListViewの特定のアイテムの背景色を変更します

繰り返しになりますが、申し訳ありませんが、コードのインデントは何らかの理由で正しく機能しませんでした。

import android.graphics.Color;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CarListActivity extends Activity {

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

final ListView carListview = (ListView) findViewById(R.id.list_view);

String[] cars = {"European Cars:", "Mercedes", "Passat", "Bently", "Porsche", "BMW", "Yugo","Land Rover",
"Japanese Cars:", "Maxima GXE", "Mazda6", "Avalon", "Toyota", "Honda", ""};

final List<String> list_of_cars = new ArrayList<String>(Arrays.asList(cars));

final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
        (this, android.R.layout.simple_list_item_1, list_of_cars);

//------------------------------------------

carListview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list_of_cars) {
// Since listViews are views, they have to be Overrdden for some things to be changed in them.
@Override
public View getView(int rowPosition, View convertView, ViewGroup parent) {

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

//------------------------------------------

// This works.  I have only tried this for two rows, the two I wanted.  I expected this line to crash the app, but it didn't.
if(getItem(rowPosition).equals("European Cars:") || getItem(rowPosition).equals("Japanese Cars:")) {

// Make the two rows have a white background color.
row.setBackgroundColor(Color.WHITE);  // this command WORKS fine by itself.

// row.setEnabled(false); this command caused "bleeding" over into other rows, so I will check for the rows in a condition.

}  // both of the getItems end here.

 else {

// All of the other rows should have this color.
row.setBackgroundColor(Color.parseColor("#EEE8AA"));
                                         // the default color

}  // else ends here.

//------------------------------------------

return row;

//------------------------------------------

}  // getView ends here.

});  // carListview.setAdapter ends here.

}  // onCreate ends here.

}  // CarListActivity ends here.

ありがとう、そしてこれが他の人に役立つことを願っています。

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ