2017-06-06 65 views
-1

我使用JSON從Web服務器獲取數據項Title和Description。我知道如何解析它,使用一個自定義適配器,並使用僅標題填充列表視圖。我希望​​它打開一個新的活動並顯示所點擊的項目的標題和描述。我是否必須創建一個新的自定義適配器來填充此列表視圖?我應該從活動的這一邊通過什麼,如何從另一邊接受?提前致謝。當在Listview上點擊標題項時獲取標題和描述android

displayTitleAdapter = new DisplayTitleAdapter(this, R.layout.rowlayout); 
    listView = (ListView)findViewById(R.id.listviewfinal); 
    listView.setAdapter(displayTitleAdapter); 
    listView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

     } 
    }); 

這是我的適配器類的getView方法

public View getView(int position, View convertView, ViewGroup parent) { 
    View row; 
    row = convertView; 
    ReqHolder reqHolder; 
    if(row == null){ 
     LayoutInflater layoutInflate = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     row = layoutInflate.inflate(R.layout.rowlayout, parent, false); 
     reqHolder = new ReqHolder(); 

     reqHolder.Title = (TextView)row.findViewById(R.id.title_title); 
     reqHolder.Requirement = (TextView)row.findViewById(R.id.req_req); 

     row.setTag(reqHolder); 
    } 
    else{ 
     reqHolder = (ReqHolder)row.getTag(); 
    } 
    //Requirements is the model class with getter and setter methods for Fields Title and Requirement. 
    Requirements requirements = (Requirements) this.getItem(position); 
    reqHolder.Title.setText(requirements.getTitle()); 
    reqHolder.Requirement.setText(requirements.getRequirement()); 
    return row; 
} 

static class ReqHolder{ 
    TextView Title; 
    TextView Requirement; 
} 

}

回答

0

該作品好:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Object o = listView.getItemAtPosition(position); 
       UserConnected str=(UserConnected) o;//As you are using Default String Adapter 
       Toast.makeText(getBaseContext(),str.getPrenom(), Toast.LENGTH_SHORT).show(); 
      } 
     }); 
+0

我用這種方法發送序列化類和@ dh19的方法來接收它,它工作!謝謝。 – Wagari

+0

不客氣 – Mohamed

0

這裏是我做到了我,請確保您的情態類實現Serializable接口。

MainActivity:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Intent i = new Intent(getApplicationContext(),Main2Activity.class); 
      i.putExtra("Question",questionArrayList.get(position)); 
      startActivity(i); 
     } 
    }); 

在我的主要活動我設置在我的列表視圖的項目上點擊監聽器,然後使用意圖類開始活動。

在我隔壁班的,我得到我投入的意圖(問題)的對象ANS顯示,我想用getter和setter從我的語氣類

Acitvity 2文本:

public class Main2Activity extends AppCompatActivity { 
@Override 
public void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activitiy_main2); 
    Question q = (Question) getIntent().getSerializableExtra("Question"); 

    TextView yourAnswer= (TextView)findViewById(R.id.title); 
    TextView correctAnswer = (TextView)findViewById(R.id.description); 

    yourAnswer.setText(q.getYourAnswer()); 
    correctAnswer.setText(q.getCorrectAnswer()); 
} 
} 

希望這能幫助你!

+0

非常感謝!有效! – Wagari

相關問題