当我选择自定义列表视图的项目时,我正在尝试启动新活动,我在模拟器上尝试了应用程序并且新活动未启动且项目无法点击......!为什么该项目不可点击?任何的想法 ?有什么建议吗?
以下是使用自定义列表视图的活动:
package com.example.welcome.madrasti;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class ListOfSchoolsPublic extends AppCompatActivity {
Intent redirect;
Intent item;
ListView list;
ArrayList<String> _sa;
DBAdapter db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_of_schools_public);
openDB();
list= (ListView) findViewById(R.id.usersList);
redirect = getIntent();
Cursor cursor = db.getAllRows();
_sa= new ArrayList<String>();
//cursor.moveToFirst();
Log.d("CURSORCOUNT","Number of rows in the Cursor is = " + String.valueOf(cursor.getCount()));
while(cursor.moveToNext()) {
if(cursor.getString(DBAdapter.COL_SChOOL_TYPE_TABLE_2).equals("حكومية"))
_sa.add(cursor.getString(DBAdapter.COL_SCHOOLNAME_TABLE_2));
}
cursor.close();
@SuppressWarnings("rawtypes")
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,_sa);
list.setAdapter(new TempLyaout(ListOfSchoolsPublic.this,_sa));
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
String selectedItem = (String) arg0.getItemAtPosition(position);
itemInfo(selectedItem);
}
});
}
///////////////////////////////////////////////////////////////
public void itemInfo(String selectedItem){
item = new Intent(getApplicationContext(), NewSchool.class);
item.putExtra("schoolName",selectedItem);
startActivity(item);
}
///////////////////////////////////////////////////////////////
@Override
protected void onDestroy() {
super.onDestroy();
closeDB();
}// end onDestroy method
///////////////////////////////////////////////////////////////
private void openDB() {
db = new DBAdapter(ListOfSchoolsPublic.this);
db.open();
} // end openDB method
//////////////////////////////////////////////////////////////
private void closeDB() {
db.close();
} // end closeDB method
}
以下是列表视图的xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ListOfSchoolsPublic">
<ListView
android:id="@+id/usersList"
android:layout_width="395dp"
android:layout_height="523dp"
android:focusable="false"
android:layout_x="4dp"
android:layout_y="-1dp"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="7dp"></ListView>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
如果您的列表项中有一些可点击的项目,则此问题会保持不变。
如果您的项侦听器不起作用,则可以在xml布局中的ListView上应用它。
android:descendantFocusability="blocksDescendants"
如果您需要适配器类(如
),也可以放置许多点击侦听器public class MyAdapter extends ArrayAdapter<Contact> {
Context context;
public MyAdapter(Context context, ArrayList<Contact> users) {
super(context, 0, users);
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
Contact user = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.listviewadapter, parent, false);
}
TextView name = convertView.findViewById(R.id.name);
TextView num = convertView.findViewById(R.id.num);
// Populate the data into the template view using the data object
name.setText(user.name);
num.setText(user.phone_number);
// can set click on name or num
name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, position + "position clicked", Toast.LENGTH_SHORT).show();
}
});
// can set click on full list item
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, position + "position clicked", Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}