Answer the question
In order to leave comments, you need to log in
How to make an inactive button in a simple list from an array (ListView)?
I made a simple array of several words and with the help of the second activity displayed detailed information about them.
I need to make one of the words inactive, i.e. not clickable. Can you tell me how to do this? Here is the code from the first activity (which lists the words in the array) and the second which displays detailed information about them.
public class MainActivity extends AppCompatActivity {
private String titles[] = {
"00. Weapon",
"01. Stigmata",
"02. Recom ,"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.listView);
listView.setAdapter(
new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, titles));
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, DetailActivity.class);
intent.putExtra("title", position);
startActivity(intent);
}
});
}
}
public class DetailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
WebView webView = findViewById(R.id.webView);
Intent intent = getIntent();
String resName = "n" + intent.getIntExtra("title", 0);
Log.i("name", resName);
Context context = getBaseContext();
String text = readRawTextFile(context, getResources().getIdentifier(resName,
"raw", "ru.alexanderklimov.manual"));
webView.loadDataWithBaseURL(null, text, "text/html", "en_US", null);
}
private String readRawTextFile(Context context, int resId)
{
InputStream inputStream = context.getResources().openRawResource(resId);
InputStreamReader inputReader = new InputStreamReader(inputStream);
BufferedReader buffReader = new BufferedReader(inputReader);
String line;
StringBuilder builder = new StringBuilder();
try {
while (( line = buffReader.readLine()) != null) {
builder.append(line);
builder.append("\n");
}
} catch (IOException e) {
return null;
}
return builder.toString();
}
}
Answer the question
In order to leave comments, you need to log in
do not use ArrayAdaper and standard layouts. Write your adapter. Pass a list of objects with text and enabled properties to it. In the method of creating a view, set the corresponding properties.
To simplify understanding, throw out the leaves, use the RecyclerView. He will make you do everything right.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question