简体   繁体   English

如何将值从列表视图传递到下一个活动

[英]How to pass value from listview to the next activity

I'm new to android studios and am currently about to finish up on my first project. 我是Android Studio的新手,目前即将完成我的第一个项目。 However, I'm stuck at the part where I have to pass the value selected from the listview to the next activity. 但是,我被困在必须将从列表视图中选择的值传递给下一个活动的部分。 I have tried researching on the codes but none seem to be able to work. 我尝试研究这些代码,但似乎都无法正常工作。 Any help would be greatly appreciated. 任何帮助将不胜感激。

DisplayProduct.java DisplayProduct.java

public class DisplayProduct extends AppCompatActivity {

ListView listView;
SQLiteDatabase sqLiteDatabase;
DatabaseHelper databaseHelper;
Cursor cursor;
ListDataAdapter listDataAdapter;

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

    listView = (ListView)findViewById(R.id.listView);
    listDataAdapter = new ListDataAdapter(getApplicationContext(),R.layout.display_product_row);
    listView.setAdapter(listDataAdapter);

    databaseHelper = new DatabaseHelper(getApplicationContext());

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            //Object data = parent.getItemAtPosition(position);
            //String value = data.toString();



        }
    });
    sqLiteDatabase = databaseHelper.getReadableDatabase();
    cursor = databaseHelper.getInformations(sqLiteDatabase);
    if(cursor.moveToFirst())
    {
        do {

            String contact,location,issue;
            contact = cursor.getString(0);
            location = cursor.getString(1);
            issue = cursor.getString(2);
            Information information = new Information(contact,location,issue);
            listDataAdapter.add(information);


        } while (cursor.moveToNext());
    }
}

} }

ListDataAdapter.java ListDataAdapter.java

public class ListDataAdapter extends ArrayAdapter {
List list = new ArrayList();
public ListDataAdapter(Context context, int resource) {
    super(context, resource);
}

static class LayoutHandler
{
    TextView Contact,Location,Issue;
}

@Override
public void add(Object object) {
    super.add(object);
    list.add(object);

}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;
    LayoutHandler layoutHandler;
    if (row == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.display_product_row, parent, false);
        layoutHandler = new LayoutHandler();
        layoutHandler.Contact = (TextView) row.findViewById(R.id.textView8);
        layoutHandler.Location = (TextView) row.findViewById(R.id.textView18);
        layoutHandler.Issue = (TextView) row.findViewById(R.id.textView90);
        row.setTag(layoutHandler);

    } else {
        layoutHandler = (LayoutHandler) row.getTag();


    }

    Information information = (Information) this.getItem(position);
    layoutHandler.Contact.setText(information.getContact());
    layoutHandler.Location.setText(information.getLocation());
    layoutHandler.Issue.setText(information.getIssue());

    return row;





}

} }

LocationDetail.java LocationDetail.java

public class LocationDetail extends AppCompatActivity {


private TextView Textv;
DatabaseHelper databaseHelper;
SQLiteDatabase sqlitedatabase;

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

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_location_detail, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

} }

From DisplayProduct.java : 从DisplayProduct.java:

Intent intent = new Intent(this, LocationDetail.class);
    Object data = parent.getItemAtPosition(position);
    String message = data.toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);

From LocationDetail.java: 从LocationDetail.java:

Intent intent = getIntent();
    String value = intent.getStringExtra(EXTRA_MESSAGE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ListView适配器从LinkedHashSet获取信息 <UserItem> 。 如何将其中一个UserItem传递给下一个活动 - ListView Adapter gets info from a LinkedHashSet<UserItem>. How to pass one of those UserItems to next activity 如何在ListView项目中进行操作并将该数据传递到android中的下一个活动? - How to do operations in ListView items and to pass that data to next activity in android? 将微调器值传递给下一个活动 - Pass Spinner value to next activity 如何将数据从一个活动传递到下一个活动 - How to pass the data from one Activity to the next activity 我想从下一个活动传递复选框值,但它会抛出NullPointerException - I want to pass checkbox value from next activity but it throws NullPointerException 如何将unique_id的值传递给要在ListView中使用的另一个活动 - How to pass value of unique_id to another activity to be used in ListView 从第一个活动到第三个活动的值传递到列表视图中将覆盖旧值 - Pass value from first activity to third activity into a listview overrides old value 如何将数据从列表视图传递到另一个活动页面? - how to pass data from listview to another activity page? 如何从listview获取变量并传递给另一个活动? - How can i get variable from listview and pass to another activity? 如何将下载的图像从一个活动传递到下一个活动? - How do I pass a downloaded image from one activity to the next?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM