我有一个ListView,我有一些主题。当我按下项目(主题)时,我无法访问My AssignaturaActivity视图。 我使用setOnItemClickListener来抛出活动,但它没有响应。
我做了自己的适配器和我自己的项目布局,所以列表显示,我可以将新主题添加到列表中,刷新它,但我无法访问项目列表... 这里有一些我的代码:
public class AssignaturesActivity extends AppCompatActivity {
private ListView llistaAssignatures;
private ArrayList<Assignatura> Assignatures;
private itemAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_assignatures);
llistaAssignatures = (ListView)findViewById(R.id.llistaAssignatures);
Assignatures = new ArrayList<Assignatura>();
Assignatures.add(new Assignatura("Android"));
Assignatures.add(new Assignatura("COMS"));
Assignatures.add(new Assignatura("IMQSA"));
Assignatures.add(new Assignatura("ANALOGICA"));
Assignatures.add(new Assignatura("ALGORITMIA A/V"));
adapter = new itemAdapter(this, R.layout.item, Assignatures);
llistaAssignatures.setAdapter(adapter);
llistaAssignatures.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(AssignaturesActivity.this, AssignaturaActivity.class);
i.putExtra("pos", position);
startActivity(i);
}
});
}
private void lanzarAcercade(View view) {
Intent i = new Intent(this, AcercaDe.class);
startActivity(i);
}
private void lanzarEditAssignaturaActivity(View view) {
Intent i = new Intent(this, EditAssignaturaActivity.class);
startActivityForResult(i, 0);
}
}
知道发生了什么事吗? 谢谢!
编辑: 我的ItemAdapter:
public class itemAdapter extends ArrayAdapter<Assignatura> { // Aprofitem els paramtres de ArrayAdapter (Herencia)
public itemAdapter(Context context,int layout_resource, ArrayList<Assignatura> data) {
//Cridar al constructor del ArrayAdapter<item> amb super.
super(context, layout_resource,R.id.nom, data);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView= super.getView(position, convertView, parent);
Assignatura item = getItem(position); //Retorna l'Item que estaba a la posicio "position"
TextView item_name = (TextView) convertView.findViewById(R.id.nom); // Busca dins de la pastilleta
RatingBar estrelles =(RatingBar) convertView.findViewById(R.id.ratingBar);
item_name.setText(item.getNomAssignatura().toString());
return convertView;
}
}
我的项目布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Assignatura"
android:id="@+id/nom"
android:textSize="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ratingBar"
android:layout_marginTop="10dp" />
</LinearLayout>
答案 0 :(得分:2)
将android:descendantFocusability =“blocksDescendants”添加到LinearLayout以查看它是否有帮助。这应该可以防止TextView和RatingBar的后代获得焦点。
答案 1 :(得分:0)
我认为您应该修改getView方法,以便回收视图。 我已经使用适配器的上下文为你的convertView添加了onClick监听器。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.your_item_layout,
parent, false);
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(context, AssignaturaActivity.class);
i.putExtra("pos", position);
context.startActivity(i);
}
});
}
Assignatura item = getItem(position); //Retorna l'Item que estaba a la posicio "position"
TextView item_name = (TextView) convertView.findViewById(R.id.nom); // Busca dins de la pastilleta
RatingBar estrelles =(RatingBar) convertView.findViewById(R.id.ratingBar);
item_name.setText(item.getNomAssignatura().toString());
return convertView;
}