0

私は現在、最終年度プロジェクトの Android アプリケーションを開発しています。でも正直なところ、私は基礎知識がなく、すべてゼロからのスタートで、オンラインのチュートリアルを参考にすることも多かったです。これが私の質問です。リストビューアクティビティからデータを取得しようとしていました。私のページには、ボタンを使用して2つのリストビューがあります。最初のリストビューは表示できましたが、2 番目のリストビューのデータを取得すると、ページが更新されるため、最初のリストビューのデータが消えます。ページ内の両方のデータを取得するには、どのコードを変更する必要がありますか? (データベースはまだ実装されていません)助けてください、どうもありがとう。以下は私のコーディングです。

XML のコーディング。

      <!-- Location -->
      <TextView android:id="@+id/TextViewLocation"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:layout_marginBottom="10dip"
            android:text="Location Information"
            android:gravity="center"
            android:textSize="15dip"
            android:textColor="#025f7c"/>

      <!--  Condition Label -->
      <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#372c24"
            android:text="Traffic Condition"/>
      <Button android:id="@+id/inputListView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:layout_marginBottom="10dip"
            android:text="choose one..."/>

      <!--  Comment Label -->
      <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#372c24"
            android:text="What's Happening?"/>
      <Button android:id="@+id/inputListView2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:layout_marginBottom="10dip"
            android:text="choose one..."/>

      <!--  Suggestion Label -->
      <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#372c24"
            android:text="Comments / Suggestion"/>
      <EditText android:layout_width="fill_parent"
            android:layout_height="80dp"
            android:layout_marginTop="5dip"
            android:layout_marginBottom="10dip"
            android:singleLine="true"/>
      <!-- Image button -->
      <Button android:id="@+id/btnImage"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:text="Upload Image"/>


      <!-- Report button -->
      <Button android:id="@+id/btnReportCheckin"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:text="Report"/>

      <!-- Link to Logout -->
      <TextView android:id="@+id/linkLogout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:layout_marginBottom="40dip"
            android:text="Log Out"
            android:gravity="center"
            android:textSize="20dip"
            android:textColor="#025f7c"/>

    </LinearLayout>
    <!-- Check or Report Form Ends -->

活動クラスのコーディング

public class CheckinActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set View to checkin.xml
    setContentView(R.layout.checkin);
    /*
    TextView LocationView = (TextView) findViewById(R.id.TextViewLocation);
    Intent h = getIntent();
    // getting attached intent data
    String address = h.getStringExtra("address");
    // displaying selected product name
    LocationView.setText(address); */ 
    Button ListViewScreen = (Button) findViewById(R.id.inputListView);
    //Listening to Button 
    ListViewScreen.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            //Switching to ListView Screen
            Intent i = new Intent(getApplicationContext(), ListViewActivity.class);
            startActivity(i);
        }
    }   );

    Button SelectedView = (Button) findViewById(R.id.inputListView);
        Intent i = getIntent();
        // getting attached intent data
        String product = i.getStringExtra("product");
        // displaying selected product name
        SelectedView.setText(product);

    Button ListView2Screen = (Button) findViewById(R.id.inputListView2);

  //Listening to Button 
    ListView2Screen.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            //Switching to ListView Screen
            Intent j = new Intent(getApplicationContext(), ListView2Activity.class);
            startActivity(j);
        }
    }   );  

    Button SelectedView2 = (Button) findViewById(R.id.inputListView2);

    Intent j = getIntent();
    // getting attached intent data
    String product2 = j.getStringExtra("product2");
    // displaying selected product name
    SelectedView2.setText(product2);



    TextView Logout = (TextView) findViewById(R.id.linkLogout);
    // Listening to Log out
    Logout.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
                            // Closing menu screen
            // Switching to Login Screen/closing register screen
            finish();
        }
    });
}
}

リストビュー クラスのコーディング

 public class ListViewActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // storing string resources into Array
        String[] traffic_condition = getResources().getStringArray(R.array.traffic_condition);

        // Binding resources Array to ListAdapter
        this.setListAdapter(new ArrayAdapter<String>(this, R.layout.listitem, R.id.listViewLayout, traffic_condition));

        ListView lv = getListView();

        // listening to single list item on click
        lv.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {
              // selected item
              String product = ((TextView) view).getText().toString();
              // Launching new Activity on selecting single List Item
              Intent i = new Intent(getApplicationContext(), CheckinActivity.class);
              // sending data to new activity
              i.putExtra("product", product);
              startActivity(i);
          }
        });
    }
}

必要に応じて、アプリのスクリーンショットを提供できます。ありがとうございます。

4

1 に答える 1

0

adapter.notifyDataSetChanged(); を呼び出してみてください。リストビューのアダプターを設定した直後。このようにして、データセット(あなたの場合はリスト)の変更がlistViewに表示されます。

この回答がお役に立てば幸いです

于 2012-11-08T08:38:04.390 に答える