onItemClickListener无法在listview上运行

时间:2017-12-23 07:02:24

标签: android sdk

我想从列表视图中打开一个新活动。在listview中打开一个新活动。我已经尝试过setOnclick监听器,但是无法正常工作。 适配器代码如下。代码中没有错误

public class MyOffers extends Activity {
static final String TAG = MyOffers.class.getSimpleName();
private View parent_view;
public String separatecheck;

private RecyclerView recyclerView;
private AdapterListBasic mAdapter;
ListView lv1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_dth_offers);


    ArrayList image_details = getListData();
      lv1 = (ListView) findViewById(R.id.dth_list);
    lv1.setAdapter(new AdapterDthList(this, image_details));
    Log.e(TAG, "hfhfhfhhfhfhhfhfofferrrrrrrrrrrrrrrrrrrrrrrrrr" );
    // listening to single list item on click
    lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Log.e(TAG, "hfhfhfhhfhfhhfhfofferrrrrrrr" );
           // Object o = lv1.getItemAtPosition(position);
          //  NewsItem newsData = (NewsItem) o;
            Intent intent=new Intent(getApplicationContext(),Dispoffer.class);
            startActivity(intent);

            // selected item


        }
    });
}

适配器:

public class AdapterDthList extends BaseAdapter {
    private ArrayList<NewsItemDth> listData;
    private LayoutInflater layoutInflater;

    public AdapterDthList(Context aContext, ArrayList<NewsItemDth> listData) {
        this.listData = listData;
        layoutInflater = LayoutInflater.from(aContext);
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
            holder = new ViewHolder();
            holder.headlineView = (TextView) convertView.findViewById(R.id.title);
            holder.reporterNameView = (TextView) convertView.findViewById(R.id.reporter);
            holder.reportedDateView = (TextView) convertView.findViewById(R.id.date);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.headlineView.setText("Amount: "+listData.get(position).getReporterName()+"/-");
        holder.reporterNameView.setText( listData.get(position).getHeadline());
        holder.reportedDateView.setText(listData.get(position).getDate());
        return convertView;
    }

    static class ViewHolder {
        TextView headlineView;
        TextView reporterNameView;
        TextView reportedDateView;
    }
}

3 个答案:

答案 0 :(得分:1)

您似乎正在使用Intent intent=new Intent(getApplicationContext(),Dispoffer.class); Intent intent=new Intent(MyOffers.this,Dispoffer.class); startActivity(intent); 上下文...尝试更改 $q = "SELECT * FROM documents WHERE uid = '$uid' ORDER BY ID DESC "; $r = mysqli_query($dbc, $q); $i = 1; while($row=mysqli_fetch_assoc($r)){ ?> <p <?php if($i == 1)echo 'style="color=red;'; ?>><?= $row['doctitle'] ?></p> </br> $i++; <?php }

+----+----+----+----+
| A  | B  | C  | D  |
+----+----+----+----+
| A1 | B1 | C1 | D1 |
| A2 | B2 | C2 | D1 |
| A3 | B3 | C3 | D1 |
+----+----+----+----+

SELECT @a = A
    ,@b = B
    ,@c = C
    ,@d = D
FROM TABLE
WHERE D = D1

SELECT @rot = @@rowcount

SET @i = 1

答案 1 :(得分:0)

如果您的列表行包含可关注或可点击的视图,则OnItemClickListener无法正常工作。

android:descendantFocusability="blocksDescendants"添加到行项目的根布局。下面是一个例子。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical" >

 // your widgets goes here

</LinearLayout>

答案 2 :(得分:0)

<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"
android:descendantFocusability="blocksDescendants"
tools:context="com.youtel.paisabox.activity.dth.MyDthOffers">
<ListView
    android:id="@+id/dth_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="10dp"
    android:layout_marginEnd="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:dividerHeight="1dp" >
</ListView>