单击ListView项目不打开新活动

时间:2017-11-01 14:17:42

标签: android listview android-activity

我有一个ListView“resultList”,但点击某个项目并未打开新的(详细)活动。我的错是什么? 谢谢!

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    this.resultList = (ListView) findViewById(R.id.resultList) ;
    this.dataSource = MyExpenseOpenHandler.getInstance(this).readAllExpenses();
    this.adapter = new ExpenseOverviewAdapter(this, dataSource);
    this.resultList.setAdapter(adapter);
    this.resultList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(final AdapterView<?> adapterView, View view, final int i, final long l) {
            Object element = adapterView.getAdapter().getItem(i);

            if (element instanceof Expense) {
                Expense expense = (Expense) element; 
                Intent intent = new Intent(MainActivity.this, ExpenseDetailActivity.class);
                intent.putExtra(ExpenseDetailActivity.EXPENSE_KEY, expense); 
                startActivity(intent);
            }

            Log.e("Click on List: ", element.toString());
        }
    });
}

3 个答案:

答案 0 :(得分:0)

您的代码似乎没问题。我认为问题是您的if条件可能返回false并且if语句中的代码没有被执行。您可以在if语句中放入一条日志消息来检查代码是否正在执行。

df['CumUniqueIDs']=df.groupby(['ID1']).ID2.apply(lambda x : x.astype('category').cat.codes.add(1))
df
Out[553]: 
  ID1 ID2  CumUniqueIDs
0   A   X             1
1   A   X             1
2   A   Y             2
3   A   Y             2
4   A   Z             3
5   B   L             1
6   B   L             1

如果您在Android监视器中看到日志消息,则可以确定if条件中的代码未执行,因此您的活动未启动。

答案 1 :(得分:0)

您是否在课程费用上实施了 Parcelable

像这样的东西

public class Expense implements Parcelable{
    private String id;
    private String name;
    private String grade;

    // Constructor
    public Expense(String id, String name, String grade){
        this.id = id;
        this.name = name;
        this.grade = grade;
   }
   // Getter and setter methods
   .........
   .........

   // Parcelling part
   public Expense(Parcel in){
       String[] data = new String[3];

       in.readStringArray(data);
       // the order needs to be the same as in writeToParcel() method
       this.id = data[0];
       this.name = data[1];
       this.grade = data[2];
   }

   @Оverride
   public int describeContents(){
       return 0;
   }

   @Override
   public void writeToParcel(Parcel dest, int flags) {
       dest.writeStringArray(new String[] {this.id,
                                           this.name,
                                           this.grade});
   }
   public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
       public Expense createFromParcel(Parcel in) {
           return new Expense(in); 
       }

       public Expense[] newArray(int size) {
           return new Expense[size];
       }
   };

}

答案 2 :(得分:0)

列表中的哪种视图,如果子视图获得焦点像按钮可能导致项目点击不能很好地工作。 您可以尝试在列表视图中添加android:descendantFocusability="beforeDescendants"