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

问题描述:

我是新来的android工作室,我正要完成我的第一个项目。但是,我卡在必须将从列表视图中选择的值传递给下一个活动的部分。我试图研究这些代码,但似乎都无法工作。任何帮助将不胜感激。如何将值从列表视图传递到下一个活动

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

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

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); 
} 

}

+3

的可能的复制[如何从一个活动传递一个对象到另一在Android](HTTP ://www.stackoverflow.com/questions/2736389/how-to-pass-an-object-from-one-activity-to-another-on-android) – petey

+0

只是使用意图,并​​把你想发送的数据使用意图额外.. – ZeroOne

从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); 

从LocationDetail.java:

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

嘿,那里,谢谢答复。我可以问你如何验证editText。我尝试键入,但无法解析符号。 –

+0

对不起,我犯了一个错误。检查编辑答案 –

+0

好吧,我刚刚尝试过。没有任何错误,但值仍然没有出现在LocationDetail.java中。任何想法为什么这可能是这种情况? –