Answer the question
In order to leave comments, you need to log in
How can I implement this?
Hello, I have a ListView which consists of 3 items. I need this element to be immediately selected at the start of the activity. And if the user clicked on another element of the list, then the one where the user clicked was highlighted. Here is what I do
in activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
<!--установил цвет выделения -->
android:listSelector="@color/item_color_test"
android:choiceMode="singleChoice" />
</RelativeLayout>
package ru.domen.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView)findViewById(R.id.listView);
String[] data = {
"a","v","c"
};
listView.setAdapter(new ArrayAdapter<String>(this, R.layout.item, data));
listView.setItemChecked(1, true);
listView.setSelection(1);
Log.v("test", "sel: " + listView.getCheckedItemPosition());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Answer the question
In order to leave comments, you need to log in
I tried many options, I can’t select it at startup, it only selects on click. Although the position of the element getCheckedItemPosition()
is correct.
The best I could find is an article that says why you shouldn't do this.
What if it's like this?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="@color/item_color_test"
android:id="@+id/listView"/>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
private final static String KEY = "selectedRow";
private MyAdapter adapter;
private int selectedItem = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView);
final String[] data = {
"a","v","c"
};
if(savedInstanceState != null && savedInstanceState.containsKey(KEY))
{
selectedItem = savedInstanceState.getInt(KEY);
}
adapter = new MyAdapter(this, R.layout.item, data, selectedItem);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selected = data[position];
selectedItem = position;
adapter.setSelectedRow(selectedItem);
}
});
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(KEY, selectedItem);
}
private class MyAdapter extends ArrayAdapter<String> {
final int resId;
int selectedRow;
public MyAdapter(Context context, int resId, String[] data, int selectedRow) {
super(context, resId, data);
this.resId = resId;
this.selectedRow = selectedRow;
selectedColor = getResources().getColor(R.color.item_color_test);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String string = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(resId, null);
}
TextView textView = (TextView) convertView;
textView.setText(string);
textView.setBackgroundColor(position == selectedRow ? selectedColor : 0x00000000);
return convertView;
}
public void setSelectedRow(int row)
{
selectedRow = row;
notifyDataSetInvalidated();
}
}
}
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:paddingLeft="10dp"/>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question